我正在尝试将Word文档中的红色文本搜索限制为单个表格单元格。无论我尝试什么,都会搜索整个文档。我有以下几点:
Sub FindRedText()
Dim MyArray() As String
Dim result As String
Dim i As Long
'Dim RngFnd As Range
i = 0
Selection.SelectCell
'Set RngFnd = Selection.Range
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorRed
Do While Selection.Find.Execute = True
'If RngFnd.InRange(RngFnd) Then
ReDim Preserve MyArray(i)
MyArray(i) = Selection
i = i + 1
'Else
'End If
Loop
If i = 0 Then
MsgBox "No Red Text"
Exit Sub
End If
For i = LBound(MyArray) To UBound(MyArray)
result = Join(MyArray, ", ")
Next i
End Sub
我留下了我的评论,以显示我将当前选择设置为范围的失败尝试。当我这样做时,单元格中的所有文本都被复制到数组中。
举个例子,说我有以下内容:
在表格单元格1,1(已选择)中:"红色文字1","蓝色文字1","红色文字2" 在表格单元格1,2中:"红色文本3"
我的宏目前放置"红色文字1","红色文字2"和"红色文字3"进入我的数组和最终结果字符串。我只想要" red text1"和"红色文字2" (即仅搜索所选单元格)。
答案 0 :(得分:0)
试试这个:首先选择区域,然后运行它。
Sub FindSelectionColorText()
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Font.Color = RGB(255, 0, 0) 'wdColorRed
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub
'如果答案有效,请标记答案和/或upvote,否则评论发生的事情。