見出し画像

区切りがある金額の下限上限をそれぞれ伏字にするよ

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

このVBAコードは、選択されたセルに入力されている「カンマや小数点で区切られた数字」を**伏字(●)**に置き換える処理を行います。例えば、セルに「1,000」や「10.5」のような金額が含まれている場合、その数字が伏字に変換されます。

仕組みの説明

  1. 画面更新を停止

    • `Application.ScreenUpdating = False` によって、処理が終わるまで画面の更新を一時的に止め、動作を速くしています。最後に、`True` に戻して、再び画面を更新します。

  2. 正規表現の設定

    • `CreateObject("VBScript.RegExp")` で、正規表現(文字列パターンを検索・置換するためのツール)を使えるようにしています。

    • `.Pattern = "(\d{1,3}([,|、|.]\d{1,3})*)"` は、数字(1~3桁の数字が、カンマや小数点、または日本語の「、」で区切られているもの)を検索するためのルールを設定しています。

  3. セルごとの処理

    • `For Each myRng In Selection` で、選択されているセルそれぞれに対して処理を行います。

    • `txt = reg.Replace(txt, "●")` によって、正規表現に一致する数字の部分を**伏字(●)**に置き換えます。

  4. エラーハンドリング

    • `On Error Resume Next` で、エラーが発生しても処理を止めずに次のセルに進むようにしています。

まとめ

このマクロを実行すると、選択範囲のセルに入力されている金額の数字部分がすべて**伏字(●)**に置き換えられます。データに金額情報が含まれている場合に、それを隠すときに便利です。

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{1,3}([,|、|.]\d{1,3})*)"
        .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 #正規表現 #伏字 #金額変換 #文字列操作 #データ隠し #セル操作 #エラーハンドリング #オブジェクト #パターンマッチ #カンマ区切り #小数点 #スクリーン更新 #コード解説 #マクロ作成 #データ処理 #金額表記 #文字列置換


English Translation

Mask Upper and Lower Bound of Separated Amounts

This explanation is created by ChatGPT.

This VBA code replaces any amounts formatted with commas or decimal points in the selected cells with a mask (●). For example, if a cell contains "1,000" or "10.5," those numbers will be replaced with a mask.

How It Works

  1. Stop Screen Updates

    • `Application.ScreenUpdating = False` temporarily halts screen updates during the process to speed up execution. The updates are restored at the end with `True`.

  2. Setting Up Regular Expressions

    • `CreateObject("VBScript.RegExp")` enables the use of regular expressions (a tool for searching and replacing patterns in strings).

    • `.Pattern = "(\d{1,3}([,|、|.]\d{1,3})*)"` defines a rule to search for numbers (1 to 3 digits separated by commas, decimal points, or the Japanese comma "、").

  3. Processing Each Cell

    • `For Each myRng In Selection` loops through each selected cell to perform the replacement.

    • `txt = reg.Replace(txt, "●")` replaces any numbers matching the regular expression with the mask (●).

  4. Error Handling

    • `On Error Resume Next` ensures that the process continues even if an error occurs.

Summary

When you run this macro, any amounts in the selected cells are replaced with a mask (●), which is useful for hiding sensitive monetary information.


Keywords

#excel #possibilities #vba #regexp #masking #currencyconversion #textmanipulation #datasecurity #celloperations #errorhandling #objectcreation #patternmatching #commaseparation #decimalpoints #screenupdates #codeexplained #macrobuilder #dataprocessing #currencyformatting #stringreplacement

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