Compile Error: End If without Block If

ptreegrdn

New Member
Joined
Jul 31, 2017
Messages
4
I'm a newbie and trying to create a macro but just cannot figure out why I keep receiving this compile error. I thought this code was fairly straightforward. Can someone please help me troubleshoot? Thanks!


Code:
Sub Copy_Incompletes() '
'
'


Dim i, Totrows As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("CA-Alta")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("CA-Jefferson")
Dim ws3 As Worksheet: Set ws3 = ThisWorkbook.Sheets("CA-Sheridan")
Dim ws4 As Worksheet: Set ws4 = ThisWorkbook.Sheets("CA-Reedley")
Dim ws5 As Worksheet: Set ws5 = ThisWorkbook.Sheets("CA-Incompletes")


Application.ScreenUpdating = False


ws1.Activate
Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2


For i = 3 To Totrows
If Cells(i, 1) = "Incomplete" Then Rows(i).Copy
'activates CA-Incompletes worksheet


ws5.Activate
Rows(3).Select
Selection.Insert Shift:=xlDown
ws1.Activate
'Copy row and paste it to the other worksheet
End If


Next i




ws2.Activate
Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2


For i = 3 To Totrows


If Cells(i, 1) = "Incomplete" Then Rows(i).Copy
'activates CA-Incompletes worksheet
ws5.Activate
Rows(3).Select
Selection.Insert Shift:=xlDown
ws2.Activate
'Copy row and paste it to the other worksheet
End If


Next i




ws3.Activate
Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2


For i = 3 To Totrows


If Cells(i, 1) = "Incomplete" Then Rows(i).Copy
'activates CA-Incompletes worksheet
ws5.Activate
Rows(3).Select
Selection.Insert Shift:=xlDown
ws3.Activate
'Copy row and paste it to the other worksheet
End If


Next i




ws4.Activate
Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2


For i = 3 To Totrows


If Cells(i, 1) = "Incomplete" Then Rows(i).Copy
'activates CA-Incompletes worksheet
ws5.Activate
Rows(3).Select
Selection.Insert Shift:=xlDown
ws4.Activate
'Copy row and paste it to the other worksheet
End If


Next i




Application.ScreenUpdating = True


End Sub
 
Last edited by a moderator:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Which line errors when you compile
 
Upvote 0
All of your Ifs are one-liners so you could 'fix' the code by removing all the End Ifs.

However I don't think that's what you want, instead try moving the code that copies onto the next line like this.
Code:
Sub Copy_Incompletes()
'
'
'
Dim i, Totrows As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("CA-Alta")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("CA-Jefferson")
Dim ws3 As Worksheet: Set ws3 = ThisWorkbook.Sheets("CA-Sheridan")
Dim ws4 As Worksheet: Set ws4 = ThisWorkbook.Sheets("CA-Reedley")
Dim ws5 As Worksheet: Set ws5 = ThisWorkbook.Sheets("CA-Incompletes")

    Application.ScreenUpdating = False

    ws1.Activate
    
    Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2

    For i = 3 To Totrows
        If Cells(i, 1) = "Incomplete" Then
            Rows(i).Copy
            'activates CA-Incompletes worksheet


            ws5.Activate
            Rows(3).Select
            Selection.Insert Shift:=xlDown
            ws1.Activate
            'Copy row and paste it to the other worksheet
        End If

    Next i

    ws2.Activate
    
    Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2

    For i = 3 To Totrows

        If Cells(i, 1) = "Incomplete" Then
            Rows(i).Copy
            'activates CA-Incompletes worksheet
            ws5.Activate
            Rows(3).Select
            Selection.Insert Shift:=xlDown
            ws2.Activate
            'Copy row and paste it to the other worksheet
        End If

    Next i

    ws3.Activate
    
    Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2

    For i = 3 To Totrows

        If Cells(i, 1) = "Incomplete" Then
            Rows(i).Copy
            'activates CA-Incompletes worksheet
            ws5.Activate
            Rows(3).Select
            Selection.Insert Shift:=xlDown
            ws3.Activate
            'Copy row and paste it to the other worksheet
        End If

    Next i

    ws4.Activate
    
    Totrows = Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count + 2

    For i = 3 To Totrows

        If Cells(i, 1) = "Incomplete" Then
            Rows(i).Copy
            'activates CA-Incompletes worksheet
            ws5.Activate
            Rows(3).Select
            Selection.Insert Shift:=xlDown
            ws4.Activate
            'Copy row and paste it to the other worksheet
        End If

    Next i

    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Perfect - the new code did the trick! Thank you!! Now what exactly did you do to fix it? As I said, I'm a newbie!
 
Upvote 0
All I did was move the highlighted line onto the next row.

BEFORE
Rich (BB code):
For i = 3 To Totrows
If Cells(i, 1) = "Incomplete" Then Rows(i).Copy
'activates CA-Incompletes worksheet


ws5.Activate
Rows(3).Select
Selection.Insert Shift:=xlDown
ws1.Activate
'Copy row and paste it to the other worksheet
End If


Next i
AFTER
Rich (BB code):
For i = 3 To Totrows
    If Cells(i, 1) = "Incomplete" Then 
        Rows(i).Copy
        'activates CA-Incompletes worksheet
        ws5.Activate
        Rows(3).Select
        Selection.Insert Shift:=xlDown
        ws1.Activate
        'Copy row and paste it to the other worksheet
    End If
Next i
 
Upvote 0

Forum statistics

Threads
1,215,628
Messages
6,125,900
Members
449,271
Latest member
bergy32204

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top