Where am I missing the Else without IF?

mrbeanyuk

Board Regular
Joined
Nov 30, 2005
Messages
213
Good morning. I have embedded a macro into Workbook_Open but in doing so I am prompted with 'Else without IF' error. It highlights the 'Else' line but I am unclear what further IF statement is needed? Any advice gratefully received.

Code:
Private Sub Workbook_Open()

Dim FR
Dim answer As Integer
Dim objInfo
Dim strLDAP
Dim strFullName
Dim LR
Set objInfo = CreateObject("ADSystemInfo")
strLDAP = objInfo.UserName
Set objInfo = Nothing
strFullName = GetUserName(strLDAP)

With Sheets("1718 KPIs")
   FR = .Cells(.Rows.Count, 2).End(xlUp).Row
    .Cells(2, 4).AutoFilter Field:=4, Criteria1:="Monthly"
 
answer = MsgBox("Would you like to go directly to your KPIs?", vbYesNo + vbQuestion, "KPIs")

If answer = vbYes Then

    With Sheets("1718 KPIs")
        LR = .Cells(.Rows.Count, 2).End(xlUp).Row
        .Cells(2, 7).AutoFilter Field:=7, Criteria1:=strFullName
     
Else
     'do nothing
End If
End With

'MsgBox "Select 'Show my KPIs'" & vbNewLine & "Once complete select 'Submit KPIs'", vbOKOnly

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
If answer = vbYes Then

    With Sheets("1718 KPIs")
        LR = .Cells(.Rows.Count, 2).End(xlUp).Row
        .Cells(2, 7).AutoFilter Field:=7, Criteria1:=strFullName
     
     End With
Else
     'do nothing
End If

just the scope of the WITH should be before the else :eek:)
 
Upvote 0
Another way, rather than using the With ... End With - which can get messy if there are a lot of nested IFs and Withs - is to set a Variable to the object you're using With for :-

Code:
Dim MySheet as Sheet

If answer = vbYes Then
    MySheet = Sheets("1718 KPIs")
    LR = MySheet.Cells(.Rows.Count, 2).End(xlUp).Row
    MySheet.Cells(2, 7).AutoFilter Field:=7, Criteria1:=strFullName
Else
     'do nothing
End If

This is more useful if you're looping through workbooks, sheets and cells/objects.
 
Upvote 0
@Johnny C
Shouldn't that be
Code:
Dim MySheet As [COLOR=#ff0000]Work[/COLOR]sheet

If Answer = vbYes Then
   [COLOR=#ff0000] Set [/COLOR]MySheet = Sheets("1718 KPIs")
    lr = MySheet.Cells(.Rows.Count, 2).End(xlUp).Row
    MySheet.Cells(2, 7).AutoFilter Field:=7, Criteria1:=strFullName
Else
     'do nothing
End If
 
Upvote 0

Forum statistics

Threads
1,215,371
Messages
6,124,529
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top