見出し画像

時給金額の異常値だけを赤字にするよ

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

このプロシージャは、Excelのセル内にある時給に関する金額が異常な値のときだけ、その部分を赤い文字に変えるものです。具体的には、1桁か2桁、または5桁以上の数字の時給金額を異常値とみなし、赤字にします。

プロシージャの仕組み

  1. 画面の更新を停止
    最初に `Application.ScreenUpdating = False` で、画面の更新を一時的に止めます。これによって、コードが実行されるときの動作が速くなります。

  2. 正規表現オブジェクトの作成
    `CreateObject("VBScript.RegExp")` で正規表現を使えるようにしています。このオブジェクトを使って、セルの値に異常な時給金額が含まれているかどうかを確認します。

  3. 正規表現のパターン設定
    `.Pattern = "時給(\d{1,2}|\d{5,})円"` では、1桁か2桁、または5桁以上の数字で終わる「時給〇〇円」を探しています。例えば、「時給8円」や「時給100000円」が該当します。

  4. 各セルのチェック
    `For Each myRng In Selection` で選択されたセルを1つずつ確認します。セルの値を `txt` に代入し、正規表現のルールに基づいて異常な時給金額がないかをチェックします。

  5. 該当部分を赤字にする
    異常な時給が見つかった場合、`myRng.Characters(match.FirstIndex + 1, match.Length).Font.Color = vbRed` で、見つかった文字列の色を赤に変更します。

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

まとめ

このコードでは、正規表現を使ってセルの内容を分析し、異常な時給金額を見つけたときだけ、その部分を赤い文字に変えます。これにより、すぐに異常値を視覚的に確認できるようになります。

Sub 時給金額の異常値だけを赤字にするよ()
    Application.ScreenUpdating = False
    Dim reg As Object
    Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
    Dim myRng As Range
    Dim txt As String
    Dim matches, match As Object
    With reg
        .Pattern = "時給(\d{1,2}|\d{5,})円"
        .IgnoreCase = True
        .Global = True
    End With

    For Each myRng In Selection
        txt = myRng.Value
        Set matches = reg.Execute(txt)
        
        If matches.count > 0 Then
            'マッチした文字列の部分だけ赤色にする
            For Each match In matches
                myRng.Characters(match.FirstIndex + 1, match.Length).Font.Color = vbRed
            Next match
        End If
    Next myRng

    Application.ScreenUpdating = True
End Sub


ハッシュタグ

#excel #vba #時給 #異常値 #セル #金額チェック #赤字 #正規表現 #セル書式 #選択範囲 #時給管理 #フォント色変更 #VBScript #異常検出 #金額判定 #オブジェクト #文字列操作 #効率化 #マクロ #できること


English Translation

Highlight Only Abnormal Hourly Rates in Red

This explanation was created with ChatGPT.

This procedure is designed to change the color of hourly rate values in Excel cells to red if they are abnormal. Specifically, it targets hourly rates that are either 1-2 digits or 5 digits or more, marking those as anomalies.

How the Procedure Works

  1. Stop screen updating
    Initially, `Application.ScreenUpdating = False` stops screen updating temporarily. This speeds up the code execution.

  2. Create the Regular Expression Object
    The line `CreateObject("VBScript.RegExp")` enables the use of regular expressions. This object will be used to check if the cell contains an abnormal hourly rate.

  3. Set Regular Expression Pattern
    The pattern `.Pattern = "時給(\d{1,2}|\d{5,})円"` searches for 1-2 digit or 5-digit numbers ending with "円". For example, "時給8円" or "時給100000円" would match this pattern.

  4. Check Each Cell
    `For Each myRng In Selection` loops through the selected cells one by one. The value of each cell is stored in `txt`, and a check is performed using the regular expression to identify any abnormal hourly rates.

  5. Change Matching Text to Red
    If an abnormal rate is found, `myRng.Characters(match.FirstIndex + 1, match.Length).Font.Color = vbRed` changes the color of the matching text to red.

  6. Restart Screen Updating
    Finally, `Application.ScreenUpdating = True` restarts the screen updating.

Summary

This code uses regular expressions to analyze the content of the cells and change the color of any detected abnormal hourly rates to red, making it easier to spot irregularities visually.



Hashtags

#excel #vba #hourlyrate #abnormalvalue #cell #amountcheck #redtext #regex #cellformat #selection #hourlyratemanagement #fontcolorchange #VBScript #anomalydetection #amountjudgment #object #textmanipulation #optimization #macro #possibilities

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