Consulting

Results 1 to 3 of 3

Thread: IF-ElseIF-Else macro seems to be disconnected

  1. #1
    VBAX Regular
    Joined
    Oct 2017
    Posts
    22
    Location

    IF-ElseIF-Else macro seems to be disconnected

    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

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    “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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Oct 2017
    Posts
    22
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •