見出し画像

金額のカンマを修正するよ

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

このプロシージャは、選択したセルの中にある金額の表記に対して、「.」や「、」で区切られている部分を「,」に置き換えるためのものです。具体的には、例えば「1.000円」や「1、000円」という形式で表記されている金額を、「1,000円」のように正しくカンマで区切られる形式に修正します。

プロシージャの仕組み

  1. 画面更新の停止
    最初に `Application.ScreenUpdating = False` と記述することで、作業中に画面がチラつかないようにしています。

  2. 正規表現オブジェクトの作成
    `CreateObject("VBScript.RegExp")` を使って、正規表現(文字列のパターンマッチングをする方法)のオブジェクトを作成しています。

  3. 正規表現の設定
    `With reg` の中で、正規表現のパターンや条件を設定しています。以下が設定内容です:

    • `Pattern = "、|."` は、「、」や「.」で区切られた数字3桁と「円」の部分を見つけるパターンです。

    • `IgnoreCase = True` で、大文字小文字の区別をしない設定にしています。

    • `Global = True` で、テキスト全体に対して置き換えを行います。

  4. セルの値を確認し、修正
    `For Each myRng In Selection` で、選択したセルの中身を一つずつ確認していきます。

    • セルの値を `txt` に取り出し、そのテキストに対して `reg.Replace(txt, ",$1")` を使って「、」や「.」をカンマに置き換えます。

    • 置き換えが完了したら、その結果を元のセルに戻します。

  5. 画面更新の再開
    最後に `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{3}円)"
        .IgnoreCase = True
        .Global = True
    End With
    On Error Resume Next
    For Each myRng In Selection
            txt = myRng.Value
            txt = reg.Replace(txt, ",$1")
            myRng.Value = txt
    Next myRng
    Application.ScreenUpdating = True
End Sub


ハッシュタグ

#excel #vba #できること #金額修正 #カンマ区切り #選択セル #文字列操作 #正規表現 #テキスト置換 #セル操作 #自動化 #効率化 #業務改善 #プログラミング初心者 #オフィス業務 #画面更新 #セル選択 #文字列検索 #カンマ挿入 #数値フォーマット


英語訳


Fixing Commas in Amounts

This explanation is created using ChatGPT.

This procedure is designed to correct the comma placement in numerical values within selected cells. It looks for amounts formatted with a period or "、" and replaces them with the correct comma placement. For example, it changes values like "1.000円" or "1、000円" to "1,000円" by inserting the proper comma.

How the procedure works:

  1. Disable Screen Updating
    First, `Application.ScreenUpdating = False` is used to prevent the screen from flickering during the operation.

  2. Create the Regular Expression Object
    The `CreateObject("VBScript.RegExp")` command creates a regular expression object, which is used to match specific text patterns.

  3. Set Up Regular Expression
    Inside the `With reg` block, the regular expression's pattern and options are set. The important settings are:

    • `.Pattern = "、|."` identifies patterns where a period or "、" is followed by three digits and the "円" symbol.

    • `.IgnoreCase = True` ensures that the matching is case-insensitive.

    • `.Global = True` applies the replacement to all matches in the text.

  4. Check and Fix Each Cell
    Using `For Each myRng In Selection`, the code loops through each selected cell.

    • The cell's content is stored in `txt`, and `reg.Replace(txt, ",$1")` replaces the incorrect punctuation with a comma.

    • The corrected text is then placed back into the cell.

  5. Re-enable Screen Updating
    Finally, `Application.ScreenUpdating = True` turns screen updating back on after the operation is complete.

This procedure helps you save time by automatically correcting the format of amounts, reducing the need for manual edits.



Hashtags

#excel #vba #whatyoucando #amountfixing #commainsertion #selectedcells #textmanipulation #regex #textreplace #celloperations #automation #efficiency #businessprocessimprovement #beginnerprogramming #officework #screenupdating #cellselection #textsearch #commainsertion #numberformatting

いいなと思ったら応援しよう!