如何在 VBA 中使用通配符更改文本格式
How to change text format using wildcards in VBA
我们的 Word 文档多次出现文本 "Division XX",其中 XX 的范围从 00 到 99。我需要使用 VBA 将它们加粗。下面是我采用的一些代码,它让我分道扬镳
With mDoc.Tables(1).Cell(1, 1).Range.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
.Execute FindText:="Division", Format:=True, ReplaceWith:="Division", Replace:=wdReplaceAll
End With
但是,它不会select或突出显示“XX”。我尝试使用通配符但失败了,因为我不清楚要为 ReplaceWith 值使用什么?
这对我有用:
Sub Tester()
With ThisDocument.Range.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True '<<**
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
'Find instances of "Division" followed by space then at least one digit
.Execute FindText:="Division [0-9]{1,}", Format:=True, Replace:=wdReplaceAll
End With
End Sub
试一试
Option Compare Text
Dim Search as Variant
For Each Search In ActiveSheet.UsedRange
If Search.Value Like "Division*" Then
Search.Font.Bold = True
End If
Next Search
希望对您有所帮助
我们的 Word 文档多次出现文本 "Division XX",其中 XX 的范围从 00 到 99。我需要使用 VBA 将它们加粗。下面是我采用的一些代码,它让我分道扬镳
With mDoc.Tables(1).Cell(1, 1).Range.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
.Execute FindText:="Division", Format:=True, ReplaceWith:="Division", Replace:=wdReplaceAll
End With
但是,它不会select或突出显示“XX”。我尝试使用通配符但失败了,因为我不清楚要为 ReplaceWith 值使用什么?
这对我有用:
Sub Tester()
With ThisDocument.Range.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True '<<**
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
'Find instances of "Division" followed by space then at least one digit
.Execute FindText:="Division [0-9]{1,}", Format:=True, Replace:=wdReplaceAll
End With
End Sub
试一试
Option Compare Text
Dim Search as Variant
For Each Search In ActiveSheet.UsedRange
If Search.Value Like "Division*" Then
Search.Font.Bold = True
End If
Next Search
希望对您有所帮助