![見出し画像](https://assets.st-note.com/production/uploads/images/146773677/rectangle_large_type_2_2c7bfa2dad37d444eebcecf27eb15a24.png?width=1200)
箇条書きの中黒だけを四角にかえるよ
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、選択したセル範囲内にあるテキストから、箇条書きの中黒を四角に置き換えるためのものです。
仕組み:
画面更新を停止します。これは、処理中に画面がちらつかないようにするためです。
正規表現オブジェクトを作成します。これは、特定のパターンに一致するテキストを見つけるために使います。
正規表現のパターンを設定します。ここでは、行の先頭にある「・」を見つけるように設定しています。
選択範囲内の各セルを順番に処理します。
セルの内容を変数に代入します。
正規表現を使って、行の先頭にある「・」を「■」に置き換えます。
セルの内容を更新します。
改行後に続く「・」も「■」に置き換えます。
画面更新を再開します。
このプロシージャは、主にテキストデータを整形するために使用されます。例えば、資料の箇条書きの形式を変更する場合などに役立ちます。
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:
Disables screen updating to prevent flickering during the process.
Creates a regular expression object to find specific patterns in text.
Sets the regular expression pattern to find bullet points at the beginning of a line.
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.
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プロシージャ #テキスト処理 #改行 #データ整形 #行の先頭 #行の途中 #選択範囲 #データ操作 #プログラミング初心者
この記事が気に入ったらサポートをしてみませんか?