我尝试在工作簿的所有工作表中搜索特定字符串“ERROR”,并将其加粗并将找到的单元格颜色设置为红色。
我可以遍历每个工作表,但无法使用VBA的Find函数。
我可以遍历每个工作表,但无法使用VBA的Find函数。
这是一个使用 Find
并格式化找到的单元格的示例
Sub FindERROR()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "ERROR"
Application.FindFormat.Clear
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' if found, remember location
FirstFound = cl.Address
' format found cell
Do
cl.Font.Bold = True
cl.Interior.ColorIndex = 3
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
' repeat until back where we started
Loop Until FirstFound = cl.Address
End If
Next
End Sub
Private Sub CommandButton1_Click()
Dim RowNum As Long
RowNum = 1
Do Until Sheets("Data").Cells(RowNum, 1).Value = ""
If InStr(1, Sheets("Data").Cells(RowNum, 2).Value, TextBox1.Value, vbTextCompare) > 0 Then
On erro GoTo next1
ListBox1.AddItem Sheets("Data").Cells(RowNum, 1).Value
ListBox1.List(ListBox1.ListCount - 1, 1) = Sheets("Data").Cells(RowNum, 2).Value
End If
next1:
RowNum = RowNum + 1
Loop
End Sub
If Not WorkBook.Sheets("Sheet1").Range("A1:Z150").Find("Cookie") Is Nothing
MsgBox "Found a Cookie"
End If
=IF(B1=C1,"Success","ERROR")
的公式,那么像Chris提到的那样使用xlFormulas
,如果您想要检查公式中的字符串。但是,如果您只关心公式的输出结果为“ERROR”(在上面的示例中,如果B1<>C1),则使用xlValues
。另一种情况:如果您有=0/0
,它会给您#DIV/0!
,那么在搜索#DIV/0!
时请使用xlValues
。 - Siddharth Rout