我需要您的帮助,通过宏可以识别是否存在一定范围内的黄色单元格(vbYellow)
Dim dataset As Range
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set dataset = Range(Cells(1, 3), Cells(lastrow, 30))
'I need something like:
if there is vbYellow in Range(Cells(1, 3), Cells(lastrow, 30)) then msgbog "Please correct error"
Else "No error found"
答案 0 :(得分:1)
也许这就是您想要的:
Sub findyellow()
Dim dataset As Range
Dim lastrow As Long
Dim cell As Range
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set dataset = Range(Cells(1, 3), Cells(lastrow, 30))
For Each cell In dataset.Cells
If cell.Interior.Color = vbYellow Then
MsgBox "Please correct error in cell (" & cell.Row & " " & cell.Column & ")"
Exit Sub
End If
Next
MsgBox "No error found"
End Sub
答案 1 :(得分:1)
或者,如果您不想潜在地循环(取决于A列中的lastrow)数千个单元格,则可以尝试使用内置SearchFormat
并检查是否还有黄色单元格:>
Sub Tst()
Dim rng1 As Range, rng2 As Range, lr As Long
With ThisWorkbook.Sheets("Sheet1")'Change accordingly
Application.FindFormat.Clear
Application.FindFormat.Interior.Color = vbYellow
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng1 = .Range(.Cells(1, 3), .Cells(lr, 30))
Set rng2 = rng1.Find(What:="", SearchFormat:=True)
If Not rng2 Is Nothing Then
MsgBox "Please correct error"
Else
'All good
End If
End With
End Sub
如果未找到满足.Find()
条件的单元,则 Nothing
将返回SearchFormat
。