+ Reply to Thread
Results 1 to 2 of 2

Else without If error

  1. #1
    Registered User
    Join Date
    07-09-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2010
    Posts
    1

    Else without If error

    Hi,

    Have what I hope is a simple problem!

    I am trying to create a spreadsheet which unhides certain rows based on what a user enters into a cell (Section 1 the cell is F20, Section 2 the cell is F40).

    I have got this to work for one section, but when adding a second section it is returning a "Else Without If" error on the third line of code.

    Appreciate if someone could tell me where I am going wrong!

    Thanks





    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Range("F20"), Target) Is Nothing Then Call Section1Code(Target)
    ElseIf Not Application.Intersect(Range("F40"), Target) Is Nothing Then Call Section2Code(Target)
    End If
    End Sub

    'Section 1
    Sub Section1Code(Target As Range)
    Range("A22:A130").EntireRow.Hidden = True


    If Range("E7").Value = "" Then

    MsgBox ("You need to enter a valuebefore you can proceed")
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Else

    If Range("E8").Value = "" Then
    MsgBox ("You need to enter a value before you can proceed")
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Else

    If Target.Value = "Yes" Then
    Range("A22:A46").EntireRow.Hidden = False
    End If
    End If
    End If
    End Sub

    'Section 2
    Sub Section2Code(Target As Range)
    Range("A50:A130").EntireRow.Hidden = True


    If Range("e27").Value = "" Then

    MsgBox ("You need to enter a valuebefore you can proceed")
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Else
    If Range("E17").Value = "" Then
    MsgBox ("You need to enter a value before you can proceed")
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Else
    If Target.Value = "Yes" Then
    Range("A50:A60").EntireRow.Hidden = False
    End If
    End If
    End If
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,568

    Re: Else without If error

    Hi iazcac,

    Welcome to the forum!!

    The way you've structured the IF statement in the worksheet_change event macro completes itself in a single line, i.e. there's no else part.

    Removing these three lines of code...

    Please Login or Register  to view this content.
    ...and replacing them with these two lines should do the trick:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1