我想在excel VBA中创建一个宏,它通过一个循环在列“B”中搜索ActiveCell的值的范围(B1:B30)。除了搜索Column之外,我还想检查日期的单元格是否使用特定颜色着色。如果日期的单元格等于设置的颜色“好”,那么我希望它将同一行的H列中单元格的颜色更改为红色。如何在Excel中选择具有设置单元格颜色的日期?
当我运行代码时,出现“运行时错误424”的错误消息:Object required。“当我去调试问题时,它突出显示了我找到的.Find函数并指向最后一个搜索行是“SearchFormat:= False”。激活“我应该怎么做才能解决这个问题? 我的整体代码的任何改进将非常赞赏。
Sub Find()
Dim FirstAddress As String
Dim MySearch As Variant
Dim Rng As Range
Dim I As Long
MySearch = Array(ActiveCell)
With Sheets("Sheet1").Range("B1:B30")
For I = LBound(MySearch) To UBound(MySearch)
Set Rng = .Find(What:=MySearch(I), _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
SearchFormat:=False).Activate
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
If ActiveCell.Style.Name = "Good" Then
Rng("H" & ActiveCell.Row).Select
Rng.Interior.ColorIndex = xlColorIndexRed
End If
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
End If
Next I
End With
End Sub
Showing the Debug mode of the run-time error.
Screenshot of the Spreadsheet for reference
ActiveCell是当前所选单元格的位置。因此MySearch将始终有任何单个值的数组。它会将LBound(MySearch)始终设置为0。我猜这就是问题所在。 – Siva