見出し画像

文章内にある時給をすべて隣列にだすよ区切り3種対応

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

このVBAプロシージャは、選択したセルの中に含まれる「時給」に関する情報を隣のセルに出力するものです。たとえば、Excelのシートに「時給1000円」や「時給1,200円」といった記述があった場合、その「時給」の部分だけを抜き出し、数字だけを隣の列に表示します。

プロシージャの流れ

  1. 画面更新停止

    • Application.ScreenUpdating = False によって処理中の画面更新を止め、作業の高速化を図っています。

  2. 正規表現オブジェクトの作成

    • VBScript.RegExp という機能を使って、テキストの中から「時給」を含む数字を探し出すための準備をします。

    • また、「数字以外の文字」を取り除くためのもう一つのオブジェクトも作成します。

  3. 正規表現パターンの設定

    • 時給パターンの正規表現では、"時給1000円" や "時給1,200円" のような書き方に対応するために、異なる3つのパターンにマッチするように設定されています。

    • 「クリーニング用の正規表現パターン」では、数字以外の文字を取り除くための設定がされています。

  4. セルの選択範囲から「時給」を抽出してクリーニング

    • 選択されたセル範囲の中から「時給〇〇円」という文字列を見つけ出し、その部分だけを隣のセルに数字だけで表示します。

  5. 出力

    • 「時給1,200円」などのパターンから、「1200」だけを隣のセルに出力します。複数の時給が1つのセルにある場合は、それぞれの値を隣のセルに順次出力します。

  6. 画面更新再開

    • 最後に Application.ScreenUpdating = True にして、画面更新を再開します。


関連リンク

Sub 文章内にある時給をすべて隣列にだすよカンマ区切り3種対応2()
    Application.ScreenUpdating = False
    
    Dim reg As Object, regClean As Object
    Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
    Set regClean = CreateObject("VBScript.RegExp") ' クリーニング用のオブジェクト作成
    
    Dim myRng As Range
    Dim txt As String
    Dim match As Object
    Dim i As Long
    
    ' 時給パターンの正規表現
    With reg
        .Pattern = "時給\d{3,4}円|時給(\d{1,3}([,、.]\d{3})*)円"
        .IgnoreCase = True
        .Global = True
    End With
    
    ' クリーニング用の正規表現パターン
    With regClean
        .Pattern = "[^0-9]" ' 数字以外の文字をすべて取り除く
        .IgnoreCase = True
        .Global = True
    End With
    
    On Error Resume Next
    For Each myRng In Selection
        txt = myRng.Value
        Set match = reg.Execute(txt)
            For i = 1 To match.count
                ' matchの結果をクリーニングして隣のセルに出力
                myRng.Offset(0, i).Value = regClean.Replace(match(i - 1).Value, "")
            Next i
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

キーワード

#excel #できること #vba #時給抽出 #正規表現 #文字列操作 #データクリーニング #VBScript #RegExp #セル選択 #隣セル出力 #複数パターン対応 #画面更新停止 #高速処理 #テキスト抽出 #数値変換 #データ整理 #オブジェクト操作 #円記号対応 #初心者向け


English Explanation

Extract Hourly Wages from Text to Adjacent Cells with Comma-Delimited Three-Pattern Support

This explanation is created by ChatGPT.

This VBA procedure extracts "hourly wage" information from selected cells and outputs them into the adjacent cells. For instance, if you have phrases like "時給1000円" or "時給1,200円" (meaning "hourly wage 1000 yen" or "hourly wage 1,200 yen"), this script will extract the numerical part and place it in the cell next to it.

How the Procedure Works

  1. Screen Updating Stopped

    • `Application.ScreenUpdating = False` pauses screen updating for faster processing.

  2. Creating Regular Expression Objects

    • Two VBScript.RegExp objects are created to extract the "hourly wage" and to clean up the extracted results (i.e., removing non-numeric characters).

  3. Setting Regular Expression Patterns

    • The hourly wage pattern matches "時給1000円", "時給1,200円," and other variations.

    • The cleaning pattern removes non-numeric characters, leaving only the wage value.

  4. Extracting and Cleaning "Hourly Wages" from Cells

    • The script goes through each selected cell, finds the wage information, and places the cleaned numeric values in the adjacent cells.

  5. Output

    • The cleaned-up values (e.g., "1200" from "時給1,200円") are output to the adjacent cells.

  6. Resuming Screen Updating

    • Finally, `Application.ScreenUpdating = True` restarts the screen updating.


Related Links


Keywords

#excel #possibilities #vba #hourlywageextraction #regex #stringoperations #datacleaning #VBScript #RegExp #cellselection #adjacentoutput #multipattern #screenupdating #fasterprocessing #textextraction #numericconversion #dataorganization #objectoperations #yensymbol #beginnerfriendly

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