简体   繁体   中英

How do I check if all numbers if a Columns are identical in excel using VBA

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

What I would like to happen is to stop running the Macro if any number other that 1 exists in Column F . If all numbers are " 1 " in Column F than continue running the code.

I figure the problem is the last bit of code below " If WorksheetFunction.CountIf(rng, myNum) = rng.Count Then MsgBox ("All the same!") "

You need to restructure your If block to determine the program flow, and use Exit Sub to exit the procedure, as per this 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!")
        Exit Sub
    End If
    Range("A:B,F:F,G:H").Delete
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM