見出し画像

文章内にある時給をすべて隣列にだすよ

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

このマクロの仕組み

このプロシージャは、Excelのセル内にある「時給○○○円」という形式のテキストを探し、その数字をセルの隣に表示するものです。たとえば、「時給1200円」と書かれたセルがあれば、その「1200」という数字だけを取り出して、隣のセルに表示します。

具体的な動き

  1. 画面更新を停止
    マクロの処理が速くなるように、まず画面の更新を一時的に止めます。これは、プロシージャの実行が終わった後に元に戻されます。

  2. 正規表現オブジェクトを作成
    `CreateObject("VBScript.RegExp")` で正規表現を使うための準備をします。正規表現は、特定のパターンに合う文字列を探すのに便利なツールです。

  3. 正規表現の設定
    `Pattern`で探す文字のパターンを設定します。今回は、「時給」で始まり、その後に3桁から4桁の数字、そして*「円」*で終わるパターン(例:「時給1200円」)を探すように設定しています。

  4. 選択範囲のセルを一つずつ確認
    `For Each myRng In Selection` で、現在選択されているセルの範囲を1つずつ確認していきます。

  5. 該当する文字列を探し、隣のセルに表示
    `reg.Execute(txt)` で、そのセルの中から「時給○○○円」に該当する部分をすべて探します。そして、その数字部分(「時給」や「円」を取り除いたもの)を隣のセルに書き出します。

  6. 画面更新を再開
    最後に、画面の更新を再開して処理を終了します。

このプロシージャを使うシチュエーション

求人情報や給与データをまとめている場合に、「時給○○○円」といった情報が書かれたセルがたくさんあるとき、その時給を一括して隣の列に表示したいときに役立ちます。

ポイント

  • 正規表現を使って「時給○○○円」というパターンを探しています。

  • 複数の「時給○○○円」がある場合、隣の列に順に表示されます。


Excel VBA リファレンス | Microsoft Learn
Excel VBA リファレンス | Microsoft Learn

この記事のYouTube動画はこちら
この記事のYouTube動画はこちら

Sub 文章内にある時給をすべて隣列にだすよ()
    Application.ScreenUpdating = False
    
    Dim reg As Object
    Set reg = 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}円"
        .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
                myRng.Offset(0, i).Value = Replace(Replace(match(i - 1).Value, "時給", ""), "円", "") ' マッチしたテキストを隣の列に出力
            Next i
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

メリット

  1. シンプルな仕組み: マクロは約20行で構成されており、シンプルな仕組みであるため、理解しやすく、保守も容易です。

  2. 複数の「時給●●円」パターンに対応: 正規表現を使って文章内の複数の「時給●●円」パターンを抽出し、それぞれを別の列に出力できるため、柔軟性が高いです。

  3. 効率的な処理: `ScreenUpdating`を`False`に設定しており、処理速度の向上を意識しています。特に大量のデータを処理する際に、画面の更新を抑えることで効率的です。

  4. Excel内で完結: Excelの範囲選択と処理が完結しており、他のツールや外部リソースを必要としないため、利便性があります。

デメリット

  1. エラーハンドリング不足: `On Error Resume Next`が使用されていますが、エラーの原因を特定しにくく、意図しない動作が発生する可能性があります。

  2. 大規模データに対するパフォーマンスの課題: 選択範囲のすべてのセルをループ処理しているため、大量のデータを扱う場合にパフォーマンスが低下する可能性があります。

  3. 正規表現パターンの制約: パターンが「時給●●円」に固定されているため、別の通貨単位や金額の表現(例: スペース付きや「¥」記号を含むもの)には対応できません。

  4. 列の数に制限: 「時給●●円」が多く含まれる場合、列が足りなくなる可能性があります。事前に列の確保が必要となり、柔軟性に欠ける面があります。

改善点・推奨事項

  1. エラーハンドリングの強化: `On Error Resume Next`の代わりに、エラーハンドリングを強化する方法を導入すると良いでしょう。特定のエラーが発生した際の処理を追加することで、デバッグが容易になります。

  2. パフォーマンスの向上: すべてのセルを一度に取得し、配列を使って処理することで、大量データへの対応を高速化できます。また、出力用の列数を事前に設定しておくことで、Excelの範囲処理を最適化できます。

  3. 正規表現パターンの柔軟性: ユーザーが必要に応じて異なるパターンに対応できるよう、正規表現パターンを引数として渡せるように改良すると、より汎用性が高くなります。

全体的に、マクロはシンプルかつ基本的なニーズを満たしており、特に中規模データの処理には適しています。より効率的で柔軟な運用を目指す場合は、上記の改善点を検討してください。


ハッシュタグ

#excel #できること #vba #正規表現 #時給 #求人情報 #データ抽出 #自動化 #効率化 #隣の列 #マクロ #セル選択 #VBScript #プログラミング初心者 #文字列操作 #データ加工 #給与データ #オブジェクト作成 #データ解析 #Excelテクニック


[English Translation]

Extracting Hourly Wages in a Sentence to the Adjacent Column

This explanation is created by ChatGPT.

How This Macro Works

This procedure searches for text in Excel cells that follow the pattern "時給○○○円" ("Hourly wage ○○○ yen") and extracts the numerical value to display it in the adjacent cell. For example, if a cell contains "時給1200円," this macro extracts "1200" and places it in the next cell.

Detailed Process

  1. Stop Screen Updating
    The macro temporarily stops screen updates to speed up processing. This will be restored at the end of the procedure.

  2. Create a Regular Expression Object
    `CreateObject("VBScript.RegExp")` sets up the use of regular expressions, which are useful for finding text patterns.

  3. Setting the Regular Expression
    The `Pattern` specifies the search pattern. In this case, it looks for text that starts with "時給" (hourly wage), followed by a 3 to 4-digit number, and ends with "円" (yen), like "時給1200円."

  4. Check Each Cell in the Selected Range
    `For Each myRng In Selection` examines each cell in the selected range.

  5. Find Matching Text and Display in the Adjacent Cell
    `reg.Execute(txt)` extracts all text matching "時給○○○円" in the cell. The numerical value (with "時給" and "円" removed) is then written to the adjacent cell.

  6. Resume Screen Updating
    Finally, the screen updating is resumed, and the process ends.

When to Use This Procedure

It's useful when working with job postings or salary data where you want to extract hourly wage information from multiple cells and display it in the adjacent column.

Key Points

  • The macro uses regular expressions to find text matching "時給○○○円."

  • If multiple matches exist, they are sequentially displayed in the adjacent column.


Excel VBA Reference | Microsoft Learn
Excel VBA Reference | Microsoft Learn

Watch the YouTube video for this article here
Watch the YouTube video for this article here


Hashtags

#excel #whatyoucando #vba #regex #hourlywage #jobinformation #dataextraction #automation #efficiency #adjacentcolumn #macro #cellselection #VBScript #beginnerprogramming #textmanipulation #dataprocessing #salarydata #objectcreation #dataanalysis #Exceltechnique

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