見出し画像

箇条書きの中黒だけを四角にかえるよ

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

このVBAプロシージャは、選択したセル範囲内にあるテキストから、箇条書きの中黒を四角に置き換えるためのものです。

仕組み:

  1. 画面更新を停止します。これは、処理中に画面がちらつかないようにするためです。

  2. 正規表現オブジェクトを作成します。これは、特定のパターンに一致するテキストを見つけるために使います。

  3. 正規表現のパターンを設定します。ここでは、行の先頭にある「・」を見つけるように設定しています。

  4. 選択範囲内の各セルを順番に処理します。

    • セルの内容を変数に代入します。

    • 正規表現を使って、行の先頭にある「・」を「■」に置き換えます。

    • セルの内容を更新します。

    • 改行後に続く「・」も「■」に置き換えます。

  5. 画面更新を再開します。

このプロシージャは、主にテキストデータを整形するために使用されます。例えば、資料の箇条書きの形式を変更する場合などに役立ちます。

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 = "^・"
        .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
            myRng = Replace(myRng, vbLf & "・", vbLf & "■")
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

Replace Bullet Points with Squares

This explanation is created with ChatGPT.

This VBA procedure is designed to replace bullet points in the selected range with squares.

How it works:

  1. Disables screen updating to prevent flickering during the process.

  2. Creates a regular expression object to find specific patterns in text.

  3. Sets the regular expression pattern to find bullet points at the beginning of a line.

  4. Processes each cell in the selected range one by one.

    • Assigns the cell content to a variable.

    • Replaces the bullet points at the beginning of a line with squares using the regular expression.

    • Updates the cell content.

    • Replaces bullet points following a line break with squares.

  5. Re-enables screen updating after the process is complete.

This procedure is particularly useful for reformatting text data, such as changing the format of bullet points in documents.

Sub ReplaceBulletPointsWithSquares()
    Application.ScreenUpdating = False
            
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'Create the object
    Dim myRng As Range
    Dim txt As String
    Dim i As Long
    With reg
        .Pattern = "^・"
        .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
            myRng = Replace(myRng, vbLf & "・", vbLf & "■")
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

ハッシュタグ:
#excel #できること #vba #箇条書き #中黒 #四角 #正規表現 #オブジェクト作成 #セル範囲 #テキスト整形 #資料作成 #VBAプロシージャ #テキスト処理 #改行 #データ整形 #行の先頭 #行の途中 #選択範囲 #データ操作 #プログラミング初心者

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