先日のセミナー
にて、ダブルスペースをシングルスペースに変換するマクロの作成をご依頼いただきました。
こういうニーズってありますね。
▼このマクロでできること
ピリオド、?、!の後に2つ以上スペースがあり、その後、半角英数字が記載されていた場合、このスペースを1つにします。
▼マクロの解説
ワイルドカードを用いた置換を実行しています。
ワイルドカードでは、半角の?と!は役割を持っていますので、?と!そのものを検索するために、\?、\!としました。
▼マクロ
Sub シングルスペース化()
Dim myRange As Range
Set myRange = ActiveDocument.Range(0, 0)
With myRange.Find
.Text = ".[ ]{2,}([a-zA-Z0-9])"
.Replacement.Text = ". \1"
.Replacement.Highlight = True
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With myRange.Find
.Text = "\?[ ]{2,}([a-zA-Z0-9])"
.Replacement.Text = "? \1"
.Replacement.Highlight = True
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With myRange.Find
.Text = "\![ ]{2,}([a-zA-Z0-9])"
.Replacement.Text = "! \1"
.Replacement.Highlight = True
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set myRange = Nothing
End Sub
▼関連記事
ワイルドカードの検索・置換の目次
マクロコードの登録方法
ツールバーのボタン登録