mardi 22 septembre 2015

VBA countif combined with if

I'm looking to use a set variable as the search criteria for a countif statement to look for "User 1" in "A:A" in sheet "Unallocated". If the countif comes back with a value of 0, I want to skip running a macro and instead carry on to the next countif (there aren't any currently but once I get this logic correct I will use it a few times over).

Here is what I have so far:

Private Sub CommandButton21_Click()

Dim iVal As Integer
Dim User As String
'Dim Wkb As Workbook 'to be used later

User = "User 1"
iVal = Application.WorksheetFunction.CountIf(Worksheets("Unallocated").Range("A2:A"), "User 1")
If iVal = 0 Then GoTo ALabel
Call User1Allo 'macro held in a module
MsgBox ("It Tried to run the macro")
ALabel:
MsgBox ("It Didn't try to run the macro")

'For Each Wkb In Workbooks 'to be used later
'        If Not Wkb.ReadOnly And Windows(Wkb.Name).Visible Then 'to be used later
'            Wkb.Save 'to be used later
'        End If 'to be used later
'    Next 'to be used later


End Sub

Any suggestions gladly welcome thank you!

Aucun commentaire:

Enregistrer un commentaire