Consulting

Results 1 to 4 of 4

Thread: Solved: 'Else without If' error...

  1. #1

    Solved: 'Else without If' error...

    Hi,

    I am running the following VBA code on Excel:

    [vba]Sub FindNewlyClosed()
    Dim iRowCount As Integer, iCounter As Integer
    Range("H2").Value = ActiveSheet.UsedRange.Rows.Count
    Range("B2").Select
    If Selection.Value = Selection.Offset(0, -1) Then Range("B3").Select
    Else: Range("A2").Select
    Selection.Insert Shift:=xlDown
    Range("B5").Select
    Selection.Font.ColorIndex = 41
    Selection.Font.Bold = True
    End Sub
    [/vba] When I try to run it, I get an error message that says "Else without If." I don't understand why I'm getting this error since the 'If' is in the line right above it. Can anyone help?

    Thanks!
    Last edited by Aussiebear; 06-23-2009 at 03:11 PM. Reason: Added VBA tags to code

  2. #2
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Give this a try..

    [VBA]
    Sub FindNewlyClosed()

    Dim iRowCount As Integer, iCounter As Integer
    Range("H2").Value = ActiveSheet.UsedRange.Rows.Count
    Range("B2").Select
    If Selection.Value = Selection.Offset(0, -1) Then
    Range("B3").Select

    Else

    Range("A2").Select
    Selection.Insert Shift:=xlDown
    Range("B5").Select
    Selection.Font.ColorIndex = 41
    Selection.Font.Bold = True
    End If
    End Sub

    [/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    AKK,

    You're missing the closing 'End If'.

    Every statement of IF or If/Else or IF/ElseIF/Else, etc needs a closing 'End If' Same with 'WITH' needs an 'End WITH', For/Loop, Do/Loop, For I = #/Next I, Select Case/End Select, etc. I always put the closing in at the same time I enter the opening then build the code in between.

    Note: VBA error messages are frequently incorrect. They say 'With' or 'For ' when it's actually and 'IF', things like that! So they can be hard to catch.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  4. #4
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Quote Originally Posted by rbrhodes
    AKK,
    Note: VBA error messages are frequently incorrect. They say 'With' or 'For ' when it's actually and 'IF', things like that! So they can be hard to catch.
    Exactly
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

Posting Permissions

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