メインコンテンツへスキップ
見出し画像

マクロに直接登録した誤字があったら赤字にして修正後を隣列にだすよ

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


    このマクロの特徴

    このマクロはマスタシートを用意する必要がありません。誤字と修正後の文字列をコード内に直接登録しているので、事前に特別な準備をしなくてもすぐに実行できます。Excelを開いてデータを入力し、マクロを実行するだけで結果を得られます。


    このマクロの仕組み

    1. 誤字と修正後の文字を直接登録
      誤字と修正後の文字列は、プログラム内にカンマ区切りで記載されています。例えば、「少しづつ」を「少しずつ」、「感心」を「関心」に修正するように設定されています。

    2. A列のデータをチェック
      ExcelシートのA列の2行目以降に入力されたデータを対象にします。各セルの中に登録された誤字が含まれていないかを調べます。

    3. 誤字が見つかった場合の処理

      • 該当する誤字部分を赤文字にして目立たせます。

      • 修正後の文字列を、右隣の列(B列)に記載します。もしすでにデータがある場合は、カンマで区切って追記します。

    4. 複数の誤字に対応
      一つのセルに複数の誤字が含まれている場合でも、それぞれを検出し赤文字にして、B列に対応する修正文字列を記録します。

    5. 処理完了後に通知
      処理がすべて完了すると、「チェック完了!」というメッセージが表示されます。


    使用例

    1. ExcelのA列に以下のようなデータを入力します:

      • 少しづつ進む

      • 時機尚早な判断

      • 耳触りな音

    2. マクロを実行すると、次のように結果が出力されます:

      • A列では、「づつ」や「耳触り」のような誤字部分が赤文字で強調されます。

      • B列には、「少しずつ」「時期尚早」「耳障り」といった修正後の文字列が表示されます。


    このマクロのポイント

    • マスタシート不要
      誤字リストと修正後リストはプログラム内に直接組み込まれているため、別途ファイルを準備する必要がありません。

    • 初心者でも扱いやすい
      プログラムを実行するだけで、すぐに結果を得られるので、Excel初心者でも簡単に利用できます。

    • どんなデータにも適用可能
      特定のフォーマットに依存せず、自由に入力されたデータに対して処理を行うことができます。


    注意点

    • 対象範囲の変更
      デフォルトではA列を対象にしていますが、他の列を対象にしたい場合は、プログラムの中で検索範囲を変更してください。

    • 誤字と修正リストの編集
      必要に応じて、プログラム内のリストを編集し、新しい誤字や修正後の文字列を追加できます。リストはカンマ区切りで記載します。

    • 実行前の確認
      データが変更されるため、元データをバックアップしておくことをおすすめします。


    関連リンク

    Sub マクロに直接登録した誤字があったら赤字にして修正後を隣列にだすよ()
        Application.ScreenUpdating = False
        Dim txt As Variant
        '誤字を半角カンマ区切りで下記にいれます
        txt = "少しづつ,時機尚早,耳触り,感心"
        txt = Split(txt, ",")
        
        Dim txt2 As Variant
        '上記の誤字を修正する文字列を半角カンマ区切りでいれます
        txt2 = "少しずつ,時期尚早,耳障り,関心"
        txt2 = Split(txt2, ",")
        
        Dim ws As Worksheet
        Set ws = ActiveSheet
        ws.Activate
        
        '検索範囲を制限(列1、行2以降)
        Dim SearchRange As Range
        Set SearchRange = ws.Range("A2:A" & ws.Cells(ws.Rows.count, 1).End(xlUp).Row)
        
        Dim FoundCell As Range, FirstCell As Range
        Dim i As Long
        For i = LBound(txt) To UBound(txt)
                Dim myStr As String
    
                Set FoundCell = SearchRange.Find(What:=txt(i))
                
                If FoundCell Is Nothing Then
                    GoTo L1
                Else
                    Set FirstCell = FoundCell
                End If
                
                Dim i2 As Long
                For i2 = 1 To Len(FoundCell.Value)
                    myStr = Mid(FoundCell.Value, i2, Len(txt(i)))
                        If myStr Like txt(i) Then
                            With FoundCell.Characters(Start:=i2, Length:=Len(txt(i))).Font
                                .ColorIndex = 3
                            End With
                        End If
                Next i2
                If FoundCell.Offset(0, 1) = "" Then
                    FoundCell.Offset(0, 1) = txt2(i)
                Else
                    FoundCell.Offset(0, 1) = FoundCell.Offset(0, 1).Value & "," & txt2(i)
                End If
                        
                Do
                    Set FoundCell = SearchRange.FindNext(FoundCell)
                    If FoundCell Is Nothing Then
                        GoTo L1
                    ElseIf FoundCell.Address = FirstCell.Address Then
                        GoTo L1
                    End If
                    For i2 = 1 To Len(FoundCell.Value)
                        myStr = Mid(FoundCell.Value, i2, Len(txt(i)))
                            If myStr Like txt(i) Then
                                With FoundCell.Characters(Start:=i2, Length:=Len(txt(i))).Font
                                    .ColorIndex = 3
                                End With
                            End If
                    Next i2
                        If FoundCell.Offset(0, 1) = "" Then
                            FoundCell.Offset(0, 1) = txt2(i)
                        Else
                            FoundCell.Offset(0, 1) = FoundCell.Offset(0, 1).Value & "," & txt2(i)
                        End If
                Loop
    L1:
        Next i
        MsgBox "チェック完了!"
        Application.ScreenUpdating = True
    End Sub
    

    ハッシュタグ

    #excel #できること #vba #誤字修正 #赤字表示 #文字列検索 #セル操作 #自動化 #エクセル初心者 #プログラミング初心者 #エクセルvba #修正機能付きvba #データ操作 #時短スキル #誤字検出 #チェック機能 #エラー検出 #業務効率化 #文字列処理 #データ検証


    英語版


    Highlighting and Correcting Typos in Excel with VBA

    This explanation is created using ChatGPT.


    Features of This Macro

    This macro does not require a master sheet. Typos and their corrections are directly registered in the code, making it easy to execute without any special preparation. Just open Excel, input data, and run the macro to see the results.


    How This Macro Works

    1. Typos and Corrections Directly Registered
      The list of typos and corresponding corrected terms are defined directly in the program using comma-separated values. For example, "少しづつ" is corrected to "少しずつ", and "感心" is corrected to "関心".

    2. Checks Data in Column A
      The macro targets the data in column A, starting from row 2. It scans each cell to check for registered typos.

    3. Processes Found Typos

      • Highlights the typo portion in red for better visibility.

      • Writes the corrected term into the adjacent column (column B). If there’s existing data, it appends the new correction separated by commas.

    4. Handles Multiple Typos in a Single Cell
      If multiple typos are found in one cell, the macro detects and highlights each typo and records all corrections in column B.

    5. Completion Notification
      Once the process is complete, a message box displays "Check Complete!"


    Example Usage

    1. Input the following data into column A:

      • 少しづつ進む

      • 時機尚早な判断

      • 耳触りな音

    2. Run the macro. The results will be as follows:

      • Column A: Typos such as "づつ" or "耳触り" will be highlighted in red.

      • Column B: Corrections like "少しずつ", "時期尚早", and "耳障り" will be displayed.


    Key Points of This Macro

    • No Master Sheet Required
      Typos and corrections are embedded in the program, so no additional files are needed.

    • Beginner-Friendly
      Just run the macro to immediately see the results—no advanced skills required.

    • Works on Any Data
      The macro is flexible and works on any input data format.


    Notes

    • Adjusting the Target Range
      By default, this macro targets column A. To target other columns, modify the range in the code.

    • Editing Typos and Corrections
      Update the typo and correction lists in the program as needed. Use comma-separated values to add new pairs.

    • Backup Before Running
      Since this macro modifies your data, it’s recommended to back up your original data before running it.


    Related Links


    Hashtags

    #excel #features #vba #typoCorrection #redHighlight #stringSearch #cellOperations #automation #excelVBA #dataProcessing #beginnerProgramming #timeSaving #typoDetection #dataVerification #errorCheck #macroTools #stringProcessing #workflowEfficiency

    あなたへのおすすめ