見出し画像

主婦の表記ゆれをなるべく整えるよ

この説明は、ChatGPTで作成しています。

このプロシージャ「主婦の表記ゆれをなるべく整えるよ」は、Excelのシート内で特定の言葉を統一的に変換するためのコードです。特に、「主婦」「主夫」などの表記揺れを、「主婦(夫)」という形に統一します。例えば、「ママパパ」や「奥さん」という言葉も一律で変換されます。

プロシージャの仕組み

  1. 画面更新を停止する
    最初に `Application.ScreenUpdating = False` で画面の更新を一時的に停止します。これは、処理の間、画面がチラつかないようにするためです。

  2. 正規表現オブジェクトの作成
    `CreateObject("VBScript.RegExp")` で正規表現を扱うためのオブジェクトを作成します。これにより、テキスト内の特定のパターンを探して、別の言葉に置き換えることができます。

  3. パターンの設定
    `With reg` ブロック内で `.Pattern` に変換対象の言葉を指定しています。このコードでは、「主婦」「主夫」「ママパパ」「しゅふ」など、複数のバリエーションが含まれています。`IgnoreCase = True` で大文字と小文字を区別せずに処理し、`Global = True` でシート全体を対象にします。

  4. 変換処理
    `For Each myRng In Selection` で選択されているセル一つ一つに対して、指定された言葉を変換していきます。 `reg.Replace(txt, "主婦(夫)")` で該当する言葉をすべて「主婦(夫)」に統一します。

  5. エラー処理
    `On Error Resume Next` を使うことで、エラーが発生してもプログラムを止めずに続行します。これは、例えば選択範囲に空のセルがあった場合などに便利です。

  6. 画面更新を再開する
    最後に `Application.ScreenUpdating = True` で画面更新を再開し、処理が終了したことを視覚的に確認できるようにします。

このプロシージャを使うことで、Excelのシート内で「主婦」「主夫」などの言葉を簡単に一括変換でき、統一感のある資料を作成できます。

Sub 主婦の表記ゆれをなるべく整えるよ()
    Application.ScreenUpdating = False
            
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'オブジェクト作成
    Dim myRng As Range
    Dim txt As String
    Dim i As Long
    With reg
        .Pattern = "主婦主夫|ママパパ|ママさん|しゅふ|シュフ|奥さん|主夫|主婦"
        .IgnoreCase = True
        .Global = True
    End With
    On Error Resume Next
    For Each myRng In Selection
            txt = myRng.Value
            txt = reg.Replace(txt, "主婦(夫)")
            myRng.Value = txt
            
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

Excel VBA リファレンス | Microsoft Learn
この記事のYouTube動画はこちら


English Translation

Adjusting Inconsistent Terms Related to “Housewife”

This explanation is created using ChatGPT.

The procedure titled Adjusting Inconsistent Terms Related to “Housewife” is a code that standardizes certain words in an Excel sheet, specifically unifying various expressions like "housewife" or "househusband" into "Housewife (husband)." For example, words like "mama and papa" or "wife" are also uniformly transformed.

How the Procedure Works

  1. Stop Screen Updating
    Initially, `Application.ScreenUpdating = False` is used to temporarily stop screen updates. This prevents screen flickering during processing.

  2. Create a Regular Expression Object
    `CreateObject("VBScript.RegExp")` creates an object for handling regular expressions. This allows searching for specific patterns in text and replacing them with other terms.

  3. Set the Pattern
    In the `With reg` block, `.Pattern` specifies the words to be transformed. This code includes multiple variations such as "housewife," "househusband," "mama and papa," etc. `IgnoreCase = True` processes without case sensitivity, and `Global = True` applies to the entire sheet.

  4. Perform the Replacement
    `For Each myRng In Selection` processes each selected cell, transforming the specified words. `reg.Replace(txt, "Housewife (husband)")` unifies all instances into "Housewife (husband)."

  5. Error Handling
    `On Error Resume Next` allows the program to continue even if an error occurs, such as encountering empty cells in the selection.

  6. Resume Screen Updating
    Finally, `Application.ScreenUpdating = True` resumes screen updates, making it visually clear that processing is complete.

By using this procedure, you can easily batch transform words like "housewife" and "househusband" in an Excel sheet, creating a more consistent document.

Excel VBA Reference | Microsoft Learn
Watch the YouTube video here


Total Character Count: 1,160


Hashtags:
#excel #できること #vba #主婦 #表記揺れ #正規表現 #テキスト変換 #セル選択 #エクセルマクロ #プログラミング #働き方 #家事 #家庭 #統一 #エクセル操作 #初心者向け #大手企業 #効率化 #VBAマクロ #主夫

この記事が気に入ったらサポートをしてみませんか?