Below is my code
Sub DeleteUneccessarySerial()
Dim rng As Range
Dim myNum As Long
myNum = 1
Set rng = Range("F:F")
Columns("A:I").Sort key1:=Range("I:I"), order1:=xlAscending, Header:=xlYes
Range("$A1:I200").AutoFilter Field:=9, Criteria1:="Checked"
If WorksheetFunction.CountIf(rng, myNum) = rng.Count Then MsgBox ("All the same!")
Range("A:B,F:F,G:H").Delete
End Sub
如果列F
中存在 1 以外的任何数字,我希望停止运行宏。如果列F
中的所有数字都是“ 1 ”,则继续运行代码。
我认为问题是下面的最后一段代码
“If WorksheetFunction.CountIf(rng, myNum) = rng.Count Then MsgBox ("All the same!")
”
答案 0 :(得分:0)
您需要重新构建If
块以确定程序流,并使用Exit Sub
退出该过程,按照以下代码:
Sub DeleteUneccessarySerial()
Dim rng As Range
Dim myNum As Long
myNum = 1
Set rng = Range("F:F")
Columns("A:I").Sort key1:=Range("I:I"), order1:=xlAscending, Header:=xlYes
Range("$A1:I200").AutoFilter Field:=9, Criteria1:="Checked"
If WorksheetFunction.CountIf(rng, myNum) = rng.Count Then
MsgBox ("All the same!")
Exit Sub
End If
Range("A:B,F:F,G:H").Delete
End Sub