If Else statement not working

Eurekaonide

Active Member
Joined
Feb 1, 2010
Messages
422
Can anyone tell me why my code below is erroring stating its got an Else without and IF please?
And why is it not looping through all my worksheets, it only puts the formula in on the first active worksheet ? Thanks ever so

VBA Code:
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
    With Ws
    If Range("O7").Value = "51" Then Ws.Range("AE2").Select
'With Selection.Interior
'.Pattern = xlSolid
'.PatternColorIndex = xlAutomatic
'.ThemeColor = xlThemeColorAccent6
'.TintAndShade = 0.799981688894314 ' green
' .PatternTintAndShade = 0
'End With
ActiveSheet.Range("2:2").AutoFilter Field:=2, Criteria1:="Actual"

Range("AE4").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(Actuals!C9,Actuals!C4,R1C1,Actuals!C8,RC1)"
Range("AE4").Select
Selection.NumberFormat = "_-* #,##0.00_-;-* #,##0.00_-;_-* ""-""_-;_-@_-"
Selection.Copy
Range(Selection, Selection.End(xlDown).Offset(-1, 0)).Select
ActiveSheet.Paste
ActiveSheet.Range("2:2").AutoFilter Field:=2
        Application.CutCopyMode = False
    ElseIf Range("O7").Value = "52" Then Ws.Range("AF2").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314 ' green
.PatternTintAndShade = 0
End With
ActiveSheet.Range("2:2").AutoFilter Field:=2, Criteria1:="Actual"

Range("AF4").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(Actuals!C9,Actuals!C4,R1C1,Actuals!C8,RC1)"
Range("AF4").Select
Selection.NumberFormat = "_-* #,##0.00_-;-* #,##0.00_-;_-* ""-""_-;_-@_-"
Selection.Copy
Range(Selection, Selection.End(xlDown).Offset(-1, 0)).Select
ActiveSheet.Paste
ActiveSheet.Range("2:2").AutoFilter Field:=2
        Application.CutCopyMode = False
    Else: Range("O7").Value = "53a"
Ws.Range("AG2").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314 ' green
.PatternTintAndShade = 0
End With
ActiveSheet.Range("2:2").AutoFilter Field:=2, Criteria1:="Actual"

Range("AG4").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(Actuals!C9,Actuals!C4,R1C1,Actuals!C8,RC1)"
Range("AG4").Select
Selection.NumberFormat = "_-* #,##0.00_-;-* #,##0.00_-;_-* ""-""_-;_-@_-"
Selection.Copy
Range(Selection, Selection.End(xlDown).Offset(-1, 0)).Select
ActiveSheet.Paste
ActiveSheet.Range("2:2").AutoFilter Field:=2
Application.CutCopyMode = False
End If
End With
Next Ws
MsgBox "Done"
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This:

Code:
If Range("O7").Value = "51" Then Ws.Range("AE2").Select

is the single line form of If that doesn't need an End If (and any Else clause needs to be on the same line). It looks like you should have the part after the Then on a new line (and the same with your ElseIf line.
 
Upvote 0
Can anyone tell me why my code below is erroring stating its got an Else without and IF please?
You have ElseIf in the 23rd line (think I counted correctly) but you have no block If preceding it.
The If in the 4th line is closed because the action is on the same line as the logical test.

This link shows you the correct ways to set out your vba If's Using If...Then...Else statements (VBA)

why is it not looping through all my worksheets,
You haven't qualified the ranges with a dot / period after the With ws line so the code is only ever working on the sheet that was active when the code started.

Also, it would help if you indented your code, it makes it much easier to read.
 
Upvote 0
Use the VBA Code Indenter Addin!
If you put a statment directly after Then, the If-statement ends with the line. No End If neccessary.
If you use Else: with colon (and mean it), always put End If or ElseIf in the next line.
 
Upvote 0
Could it be you got an
"With ..." in the third that doesn't have an "End With"

I am new to this so also learning...

Dan
 
Upvote 0
@Chiwidan, this thread is nearly a year old. Not sure why it is being addressed, but there is an 'End With' for the third line, it is 3 lines up from the 'End sub' at the bottom of the code. The 'Else without If error' is exactly correct. the only previous 'If' in the code has a 'then' and instruction after it, thus ending that 'If' clause, as @RoryA and @jasonb75 stated.
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,694
Members
449,117
Latest member
Aaagu

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