none
Getting a Compile Error on an If-ElseIf statement RRS feed

  • Question

  • Trying to learn VBA and getting a compile error of "Else without If" in the following code.  Can anyone help?

        

        If CountPD = CountOD Then Stop
        ElseIf CountPD > CountOD Then Sheets("PivotData").Rows("10:10").Select.Delete Shift:=xlUp
        ElseIf CountPD < CountPD Then Sheets("PivotData").Rows("10:10").Select.Insert Shift:=xlUp

        End If

    My variables, CountPD and CountOD, has been declared and defined. Currently CountPD = 19 and CountOD =20. I was hoping the third of the above If statement would get executed but I get the compile error as soon as I get to the second If statement.

    Friday, March 11, 2016 9:03 PM

Answers

  • At the least, you need a carriage return between Then and Stop (but VBA does not use Stop except for debugging), and you need to Shift xlDown when inserting, not xlUp and you need to compare CountPD to CountOD in your third line, and .Select is not allowed....

    So, try it like this:

        If CountPD = CountOD Then
            Stop  'for debugging only
            'End  'Stop all code
            'Exit Sub   ' instead of End - stop just the subroutine
        ElseIf CountPD > CountOD Then
            Sheets("PivotData").Rows("10:10").Delete Shift:=xlUp
        ElseIf CountPD < CountOD Then
            Sheets("PivotData").Rows("10:10").Insert Shift:=xlDown
        End If





    Friday, March 11, 2016 9:58 PM

All replies

  • At the least, you need a carriage return between Then and Stop (but VBA does not use Stop except for debugging), and you need to Shift xlDown when inserting, not xlUp and you need to compare CountPD to CountOD in your third line, and .Select is not allowed....

    So, try it like this:

        If CountPD = CountOD Then
            Stop  'for debugging only
            'End  'Stop all code
            'Exit Sub   ' instead of End - stop just the subroutine
        ElseIf CountPD > CountOD Then
            Sheets("PivotData").Rows("10:10").Delete Shift:=xlUp
        ElseIf CountPD < CountOD Then
            Sheets("PivotData").Rows("10:10").Insert Shift:=xlDown
        End If





    Friday, March 11, 2016 9:58 PM
  • Thanks. That got rid of the compile error. What's a carriage return? 

    Also, even though I'm not getting an error, my code isn't executing. I was expecting a line to inserted on row 10 but for some reason nothing happens.

    Friday, March 11, 2016 10:07 PM
  • A carriage return is when you press Enter in the editor - the entry then moves to the next line.  The name is based on old manual typewriters, where the Carriage was the assembly that held the paper and moved it beneath the striking keys.  When you did a carriage return you actually moved (returned) the carriage physically back to its starting point and moved the paper up to type onto the next line (which is why vbCRLF is named what it is....).

    Anyway, try my code - you were using 

     ElseIf CountPD < CountPD 

    and not

     ElseIf CountPD < CountOD

    and you cannot have .Select in there either.




    Friday, March 11, 2016 10:12 PM
  • Thank you. The code is working now. 
    Monday, March 14, 2016 5:12 PM