如何使用VBA检查Excel中的所有数字是否相同

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 

我想要发生的是停止运行macros,如果任何其他数字1存在F 。 如果F列中的所有数字均为“ 1 ”,则继续运行代码。

我想问题是下面的代码的最后一位“ If WorksheetFunction.CountIf(rng, myNum) = rng.Count Then MsgBox ("All the same!")

您需要重构您的If块以确定程序stream,并按照以下代码使用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