PDA

View Full Version : [SOLVED] IF-ElseIF-Else macro seems to be disconnected



thk12205
03-07-2018, 09:37 AM
Version: 2013 Professional Plus

I am trying to run a basic If, ElseIf, End If script, however the If and Else If seem to not be connected to eachother.
The error I run into is “Compile Error: Else without If”. If I delete the script underneath the IF statement, the error disappears.
Is there something in the body of the If statement that is stopping If and ElseIf from connecting?



Sub MoveButtons()
Application.ScreenUpdating = False
Dim rng As Range


If Columns("H").Hidden = True And Columns("K").Hidden = True And Columns("O").Hidden = True Then ActiveSheet.Range("A1") = 1

Set rng = ActiveSheet.Range("P4")
With ActiveSheet.Shapes("Button 3")
.Top = rng.Top
.Left = rng.Left
.IncrementLeft 0
Set rng = ActiveSheet.Range("P4")
With ActiveSheet.Shapes("Button 7")
.Top = rng.Top
.Left = rng.Left
.IncrementLeft 30
Set rng = ActiveSheet.Range("P4")
With ActiveSheet.Shapes("Button 9")
.Top = rng.Top
.Left = rng.Left
.IncrementLeft 60
End With

ElseIf Columns("H").Hidden = False And Columns("K").Hidden = True And Columns("O").Hidden = True Then
ActiveSheet.Range("A1") = 2

Set rng = ActiveSheet.Range("J4")
With ActiveSheet.Shapes("Button 2")
.Top = rng.Top
.Right = rng.Right
.IncrementLeft
Set rng = ActiveSheet.Range("P4")
With ActiveSheet.Shapes("Button 7")
.Top = rng.Top
.Left = rng.Left
.IncrementLeft
Set rng = ActiveSheet.Range("P4")
With ActiveSheet.Shapes("Button 9")
.Top = rng.Top
.Left = rng.Left
.IncrementLeft 30
End With

Else
MsgBox "Error"

End If
Application.ScreenUpdating = True

SamT
03-07-2018, 09:54 AM
“Compile Error: Else without If”.
Also occurs when you have other open Bracket Expressions inside the If... End If

In your case, 4 With's without End With's

thk12205
03-07-2018, 10:04 AM
I thought I'd caught the End With earlier, but it seems I missed twice as many as I'd caught!
Thank you for the quick response.