見出し画像

夫がついていない主婦だけを数えるよ

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

このプロシージャは、選択された範囲内にあるテキストの中から、"夫"がついていない*「主婦」*という単語を探して、その数をカウントするプログラムです。具体的には、正規表現という仕組みを使って、条件に合った「主婦」を探し出しています。ここでは、VBScriptの正規表現オブジェクトを使っており、単純な検索よりも細かい条件で文字列を探せます。

プロシージャの仕組み

  1. Application.ScreenUpdating = False
    画面の更新を一時的に止めて、動作を速くします。この部分は、処理が終わった後に再開されます。

  2. CreateObject("VBScript.RegExp")
    これは正規表現の機能を使うための準備です。正規表現は、特定のパターンに一致する文字列を見つけるためのものです。

  3. Pattern設定 ("主婦(?!(夫))")
    この部分では、「主婦」という単語に続いて「(夫)」がついていないものを探すように設定しています。

    • 「主婦」という単語を見つけます。

    • 「?!」は、直後に「(夫)」がないという条件を意味します。

  4. Selectionのループ処理
    選択された範囲(セル)を一つずつ見ていきます。各セルの中にあるテキストから、設定した正規表現に一致する「主婦」を探します。

  5. matcheCount 変数
    それぞれのセルに見つかった「夫がついていない主婦」の数を数えます。

  6. MsgBoxで結果を表示
    各セルで見つかった「夫がついていない主婦」の数を表示します。

このプログラムは、主婦に関するデータが含まれている場合に便利です。例えば、アンケート結果などの中から、夫の表記がない主婦だけを抽出したいときに使えます。


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

Sub 夫がついていない主婦だけを数えるよ()
    Application.ScreenUpdating = False
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'オブジェクト作成
    Dim myRng As Range
    Dim txt As String
    Dim matches As Object
    Dim matcheCount As Long
    With reg
        .Pattern = "(主婦(?!\(夫\)))"
        .IgnoreCase = True
        .Global = True
    End With
    On Error Resume Next
    For Each myRng In Selection
            matcheCount = 0
            txt = myRng.Value
            Set matches = reg.Execute(txt)
            If matches.count > 0 Then
                matcheCount = matcheCount + matches.count
            End If
            MsgBox "主婦の件数:" & matcheCount
    Next myRng
    Application.ScreenUpdating = True
End Sub

キーワード

#excel #できること #vba #主婦 #正規表現 #セル範囲 #テキスト検索 #パターンマッチ #夫がいない #マクロ #VBScript #データ処理 #ワイルドカード #正規表現オブジェクト #選択範囲 #データ分析 #ループ処理 #MsgBox #アンケート処理 #検索条件 #動作高速化


English Translation:

Counting Only Housewives Without Husbands

This explanation is created with ChatGPT.

This procedure is a program that counts the number of "housewives" without a mention of "husband" in the selected text range. Specifically, it uses a feature called regular expressions to find the matching pattern. Here, we use a VBScript regular expression object, which allows for more detailed text searching compared to simple methods.

How the Procedure Works

  1. Application.ScreenUpdating = False
    Temporarily stops screen updating to speed up the process. The screen update will resume after the process ends.

  2. CreateObject("VBScript.RegExp")
    This prepares to use the regular expression functionality. Regular expressions help find text that matches specific patterns.

  3. Pattern Setting ("主婦(?!(夫))")
    This part sets the pattern to find "主婦" (housewife) that is not followed by "(夫)" (husband).

    • Looks for the word "主婦" (housewife).

    • The ?! means that the following text must not include "(夫)" (husband).

  4. Loop Through Selection
    The program checks each cell in the selected range. From each cell's text, it searches for housewives that match the specified regular expression.

  5. matcheCount Variable
    This variable counts how many instances of "housewife without husband" are found in each cell.

  6. MsgBox to Display Results
    For each cell, the program shows a message box displaying the count of housewives without husbands.

This program is useful when dealing with data about housewives. For instance, you can extract only those housewives who are not described as having a husband from survey results.


Excel VBA Reference | Microsoft Learn
Watch the YouTube video for this article here


Keywords

#excel #possibilities #vba #housewife #regex #cellrange #textsearch #patternmatch #withoutahusband #macro #VBScript #dataprocessing #wildcard #regexobject #selectionrange #dataanalysis #loopprocessing #MsgBox #surveyprocessing #searchcriteria #optimizeperformance

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