Compile Error Next With For

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any suggestions and I will give feedback on them.

I get the following error "Compile error: Next without for on

Code:
Next i

Code:
Sub ClearCFData()
    
    'Dimensioning
        Dim i As Integer
        Dim RowNumber As Long
    
    'Turn Off Screen Updating
        Application.ScreenUpdating = False
    
    'Activating Sheet1
        Worksheets("Sheet1").Activate
    
    'Selecting Data and Clearing Values
            
        'Setting the Starting Value of the RowNumber
            RowNumber = 4
            
        'Start Loop
            For i = 1 To 7
                

                    Range("B" & RowNumber & ":WD" & (RowNumber + 2)).ClearContents
                    

                    RowNumber = RowNumber + 4
                    Range("B" & RowNumber & ":WD" & (RowNumber + 2)).ClearContents
    

                    RowNumber = RowNumber + 4
                    Range("B" & RowNumber & ":WD" & (RowNumber + 2)).ClearContents
    

                    RowNumber = RowNumber + 4
                    Range("B" & RowNumber & ":WD" & (RowNumber + 1)).ClearContents
                    

                    RowNumber = RowNumber + 5
                    Range("B" & RowNumber & ":WD" & (RowNumber + 1)).ClearContents
                        

                    RowNumber = RowNumber + 15
                    Range("B" & RowNumber & ":WD" & (RowNumber + 1)).ClearContents
                    
                
                'Setting the RowNumber for the Next Set of Data
                        
                        If i <= 3 Then
                            RowNumber = RowNumber + 8
                    
                        If i = 4 Then
                            RowNumber = 164
                        
                        If i > 4 And i < 8 Then
                            RowNumber = RowNumber + 80
                            
                        End If
                        
                Next i
            
  
            
        'Puts the ending mark back
            Range("WF28").Copy Range("WF1:WF482")
                    
        'Sets the cursor at the begining
            Worksheets("Sheet1").Activate
            Range("B4").Select
            
        
        'Turn On Screen Updating
            Application.ScreenUpdating = True


        'Calculate and Turn on Automatic Calculation
            Calculate
            Application.Calculation = xlAutomatic


End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
The problem is here
Code:
                        If i <= 3 Then
                            RowNumber = RowNumber + 8
                    
                        If i = 4 Then
                            RowNumber = 164
                        
                        If i > 4 And i < 8 Then
                            RowNumber = RowNumber + 80
                            
                        End If
You have 3 If statements, but only one End If
 
Upvote 0
You have three nested If statements and only one End If line. You should use ElseIf clauses instead:

Code:
                        If i <= 3 Then
                            RowNumber = RowNumber + 8
                    
                        ElseIf i = 4 Then
                            RowNumber = 164
                        
                        ElseIf i > 4 And i < 8 Then
                            RowNumber = RowNumber + 80
                            
                        End If
 
Upvote 0
Thanks Fluff (Post# 2) and RoryA (Post# 3)!

That was the issue and the fix worked! Complete oversight by me!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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