見出し画像

数字だけを伏字にするよ

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

このマクロの概要

このマクロは、Excelシート上で選択したセルの中から、数字だけを「●」に置き換えるためのものです。たとえば、「電話番号」や「価格」など、セル内に含まれている数字を他の人に見せたくない場合などに使えます。

仕組みの解説

  1. 画面更新の停止

    • `Application.ScreenUpdating = False`:処理が終わるまで画面を更新しないようにしています。これにより、マクロの動作が速くなります。

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

    • `Set reg = CreateObject("VBScript.RegExp")`:正規表現という特殊なパターンマッチングの方法を使うために必要な準備です。これにより、テキストの中から数字を探して置き換えることができます。

  3. 正規表現の設定

    • `.Pattern = "\d"`:正規表現のパターンを設定します。`\d`は*「数字」*を意味します。

    • `.IgnoreCase = True`:大文字・小文字の区別はしない設定ですが、今回は数字を対象とするのであまり関係はありません。

    • `.Global = True`:すべての数字を対象にする設定です。

  4. 選択範囲のセルを順番に処理

    • `For Each myRng In Selection`:選択したセル範囲の中から一つずつセルを取り出して処理します。

    • `txt = myRng.Value`:現在のセルの値を変数`txt`に保存します。

    • `txt = reg.Replace(txt, "●")`:`txt`の中にあるすべての数字を「●」に置き換えます。

    • `myRng.Value = txt`:置き換えが終わったテキストを元のセルに戻します。

  5. 画面更新の再開

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

注意点

  • このマクロは、選択したセル範囲に対してのみ実行されます。

  • セル内の数字以外の文字はそのまま残ります。

実際の活用例

  • 社外秘の資料を見せる前に、数値データだけを伏字にするなど、情報を隠したい場合に役立ちます。

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

ハッシュタグ

#excel #できること #vba #マクロ #正規表現 #テキスト置換 #伏字 #数字置換 #オブジェクト作成 #選択範囲 #スクリプト #自動化 #データ加工 #業務効率化 #文字列操作 #プログラミング #簡単VBA #Excelマクロ #セル操作 #情報隠し


Mask Only Numbers

This explanation is created by ChatGPT.

Overview of the Macro

This macro is designed to replace only the numbers in the selected cells in an Excel sheet with "●". For example, it's useful if you want to hide numbers like phone numbers or prices from others.

How It Works

  1. Stopping Screen Updates

    • `Application.ScreenUpdating = False`: This line stops the screen from updating until the process is complete, making the macro run faster.

  2. Creating the Regular Expression Object

    • `Set reg = CreateObject("VBScript.RegExp")`: This sets up the use of regular expressions, allowing the macro to find and replace numbers within the text.

  3. Setting the Regular Expression

    • `.Pattern = "\d"`: This defines the pattern to search for. `\d` stands for digits (numbers).

    • `.IgnoreCase = True`: This ignores case sensitivity, but since we're dealing with numbers, it doesn't affect the result.

    • `.Global = True`: This means all instances of numbers will be found and replaced.

  4. Processing Each Cell in the Selected Range

    • `For Each myRng In Selection`: This loop goes through each cell in the selected range.

    • `txt = myRng.Value`: The current cell's value is stored in `txt`.

    • `txt = reg.Replace(txt, "●")`: All numbers in `txt` are replaced with "●".

    • `myRng.Value = txt`: The modified text is placed back into the cell.

  5. Resuming Screen Updates

    • `Application.ScreenUpdating = True`: Screen updating is resumed after the macro finishes.

Important Points

  • This macro works only on the selected range of cells.

  • Non-numeric characters in the cells remain unchanged.

Practical Uses

  • It’s helpful when you want to mask sensitive numeric data in a report before sharing it.

Excel VBA Reference | Microsoft Learn
YouTube Video of This Article

Hashtags

#excel #whaticando #vba #macro #regex #textreplacement #masking #numberreplacement #objectcreation #selectionrange #scripting #automation #dataprocessing #workefficiency #stringoperations #programming #simpleVBA #ExcelMacro #celloperations #datahiding

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