每个值为 0 的单元格,该行都将被隐藏。任何超出最小值和最大值的值都将显示为红色。
Set mr = Sheets("COA").Range("i11:i34")
For Each cell In mr
If cell.DisplayFormat.Interior.Color = vbRed Then
MsgBox
该代码与预期不符,因为红色隐藏单元格也被计算在内。
我之前问过,人们回答了
Set rng = Range("Your range").SpecialCells(xlCellTypeVisible)
但是,如果我有两个条件怎么办?如何组合 set 和 if?
条件 A = 如果事件单元格为红色,则返回用户表单并
条件 B = 如果事件单元格不是红色,则卸载用户表单
Set Rng = Range("i11:i34").SpecialCells(xlCellTypeVisible)
If ActiveCell.DisplayFormat.Interior.Color = vbRed Then
MsgBox "bla bla bla", vbCritical + vbOKOnly, ""
'Show Excel and resize the UserForm2
Application.Visible = True
Me.Height = 405
Me.Width = 730.5
End If
If ActiveCell.DisplayFormat.Interior.Color = 0 Then
MsgBox "bla bla bla", vbCritical + vbOKOnly, ""
End If
最佳答案
这就是你正在尝试的吗?
请记住,编写清晰简洁的代码总是更好。最好也充分限定您的对象。还有一件事。使用 SpecialCells
时,始终将它们夹在 On Error Resume Next
和 On Error GoTo 0
之间,如下所示,否则您可能会收到错误。
顺便说一句,我没有使用If-Then-Else
。我使用了选择案例
。
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Dim VisibleCells As Range
Dim aCell As Range
'~~> Change this to the relevant sheet
Set ws = Sheet1
Set rng = ws.Range("I11:I34")
On Error Resume Next
Set VisibleCells = rng.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not VisibleCells Is Nothing Then
For Each aCell In VisibleCells
Select Case aCell.DisplayFormat.Interior.Color
Case vbRed
MsgBox "Blah1", vbCritical + vbOKOnly, ""
'Show Excel and resize the UserForm2
'Application.Visible = True
'Me.Height = 405
'Me.Width = 730.5
Case 0
MsgBox "Blah2", vbCritical + vbOKOnly, ""
'~~> For no color use the below
'Case 16777215
'MsgBox "Blah3", vbCritical + vbOKOnly, ""
End Select
Next aCell
End If
End Sub
关于excel - 组合 "Set"和 "If"来计数非隐藏的红细胞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74596963/