見出し画像

金額の下限上限をすべて伏字にするよ

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

このプロシージャは、Excelシート内のセルに書かれている「〇〇円~〇〇円」といった金額の範囲を自動的に「●円」に置き換えるものです。具体的には、選択したセルの中から金額の範囲(例えば「500円~1000円」など)を探して、その部分を伏字「●円」に変えてくれます。

プロシージャの仕組み

  1. 画面の更新を一時停止
    最初に `Application.ScreenUpdating = False` で、処理中の画面のチラつきを防ぐために、Excelの画面更新を止めています。

  2. 正規表現オブジェクトの作成
    `CreateObject("VBScript.RegExp")` という命令で、「正規表現」という仕組みを使います。これにより、特定のルールに従った文字列(今回の場合は「〇〇円~〇〇円」)を見つけ出し、操作することができます。

  3. パターンの設定
    `With reg` という部分で、「\d+円~\d+円」というパターンを設定しています。この「\d+」は「数字」を意味しており、例えば「500円~1000円」といった形式の文字列を見つけ出すためのルールを作っています。

  4. 選択範囲のセルをチェック
    `For Each myRng In Selection` で、選択されたセルを一つずつ確認します。各セルの値を `txt` という変数に一時的に保存し、その中の「〇〇円~〇〇円」を探して伏字に置き換えます。

  5. 正規表現で置換
    `reg.Replace(txt, "●円")` という部分で、見つけた「〇〇円~〇〇円」の部分を「●円」に変換しています。

  6. 結果をセルに反映
    最後に、伏字に置き換えた結果を元のセルに戻しています。

  7. 画面の更新を再開
    最後に `Application.ScreenUpdating = True` で、画面の更新を再開します。

このプロシージャを実行すると、選択した範囲にある「〇〇円~〇〇円」という金額の範囲がすべて「●円」に変わります。簡単に金額を伏字にしたい時に便利です。

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 = "\d+円~\d+円"
        .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 #金額 #伏字 #置換 #正規表現 #範囲選択 #セル操作 #自動化 #プログラミング #VBScript #VBA入門 #文字列操作 #テキスト処理 #Excel自動化 #初心者向け #簡単 #作業効率化 #金額処理


英語翻訳

Replace All Lower and Upper Limit Amounts with Symbols

This explanation is created using ChatGPT.

This procedure replaces any range of amounts (e.g., "¥500 to ¥1000") within selected cells in an Excel sheet with a placeholder, "●円". Specifically, it searches for ranges of amounts in the format "XXX円~YYY円" and replaces those with "●円".

How the Procedure Works

  1. Temporarily Stop Screen Updates
    The command `Application.ScreenUpdating = False` prevents screen flickering by stopping Excel from refreshing the screen during the process.

  2. Create Regular Expression Object
    The line `CreateObject("VBScript.RegExp")` creates an object for using regular expressions. This is a way to search for patterns in text, such as ranges of amounts ("XXX円~YYY円").

  3. Set the Pattern
    In the `With reg` section, the pattern `"\d+円~\d+円"` is set. The `\d+` represents "numbers", so this pattern looks for strings like "500円~1000円".

  4. Check Each Selected Cell
    The loop `For Each myRng In Selection` goes through each selected cell. The cell value is temporarily stored in the `txt` variable, and the range of amounts (if found) is replaced with the symbol "●円".

  5. Replace with Regular Expression
    The line `reg.Replace(txt, "●円")` does the actual replacement, converting any found range (like "XXX円~YYY円") into "●円".

  6. Update the Cell Value
    After the replacement, the result is put back into the original cell.

  7. Resume Screen Updates
    Finally, `Application.ScreenUpdating = True` resumes the normal screen updating process.

When you run this procedure, all the range amounts formatted as "XXX円~YYY円" in the selected cells will be replaced with "●円". It’s useful for cases where you need to quickly anonymize amounts.


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