Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Ankeny, Iowa, USA
    Posts
    344
    Thanks
    8
    Thanked 0 Times in 0 Posts

    ElseIf in Macro (Excel 2002)

    Hi,
    I can't get my macro (below) to work... it's giving me a "else without an if" error message. I have an "if" so I'm confused... obviously I'm missing something??? I have the actual Excel formula as a comment within the macro. Any direction on what I've done wrong would be greatly appreciated!
    Thanks!
    Lana

    Sub ActionNeeded()

    Dim lngRow As Long


    Do While Sheets("Data").Range("A" & lngRow) <> ""

    '=IF(COUNTIF($H$2:$H$15000,H3)=1,"Keep",IF(SUMIF($ H$2:$H$15000,H3,$G$2:$G$15000)=0,"Delete","Fix"))


    If Application.WorksheetFunction. _
    CountIf(Sheets("Data").Range("H2:H15000"), _
    Sheets("Data").Range("H" & lngRow)) = 1 Then _
    Sheets("Data").Range("I" & lngRow) = Keep

    ElseIf Application.WorksheetFunction. _
    SumIf(Sheets("Data").Range("H2:H15000"), _
    Sheets("Data").Range("H" & lngRow), _
    Sheets("Data").Range("G2:G15000")) = 0 Then _
    Sheets("Data").Range("I" & lngRow) = Delete

    Else
    Sheets("Data").Range("I" & lngRow) = Fix

    End If

    lngRow = lngRow + 1

    Loop


    End Sub

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ElseIf in Macro (Excel 2002)

    You had several problems. First, you had an Underscore line continuation marker after each Then which turned the first If statement into a single line If that does not require and End If. The second problem was that what I think were supposed to be strings "Delete", "Keep", and "Fix" did not have quote marks around them. Since Fix is a function, that makes it a reserved word and that was causing a syntax error in that line. See if the following works better.

    <code>
    Sub ActionNeeded()

    Dim lngRow As Long


    Do While Sheets("Data").Range("A" & lngRow) <> ""

    '=IF(COUNTIF($H$2:$H$15000,H3)=1,"Keep",IF(SUMIF($ H$2:$H$15000,H3,$G$2:$G$15000)=0,"Delete","Fix"))


    If Application.WorksheetFunction. _
    CountIf(Sheets("Data").Range("H2:H15000"), _
    Sheets("Data").Range("H" & lngRow)) = 1 Then
    Sheets("Data").Range("I" & lngRow) = "Keep"

    ElseIf Application.WorksheetFunction. _
    SumIf(Sheets("Data").Range("H2:H15000"), _
    Sheets("Data").Range("H" & lngRow), _
    Sheets("Data").Range("G2:G15000")) = 0 Then
    Sheets("Data").Range("I" & lngRow) = "Delete"

    Else
    Sheets("Data").Range("I" & lngRow) = "Fix"

    End If

    lngRow = lngRow + 1

    Loop


    End Sub
    </code>
    Legare Coleman

  3. #3
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Ankeny, Iowa, USA
    Posts
    344
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: ElseIf in Macro (Excel 2002)

    Legare,
    Thanks for your help! I was excited to see that it was just a few errors! Sometimes I'm way off base. I love this stuff! Thanks again for your help... I appreciate your explanation too, it helps me understand why it was wrong.
    Lana

Posting Permissions

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