Next without for

Dipmala Patel

Board Regular
Joined
Jan 24, 2008
Messages
75
I have following code, but everytime try to run it give me error mess saying "Next Without For". I dont know what am I doing wrong. Thank You.



Sub CopyContractsDemo()
Dim LstRw As Long, _
ContractRng As Range, _
ContractNum As Range, _
ThsSht As String, _
Sht As Worksheet, _
ShtExists As Boolean
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False

ThsSht = ActiveSheet.Name
With Sheets(ThsSht)
LstRw = .Cells(Rows.Count, "A").End(xlUp).Row

'''/// Set the range of contract numbers
Set ContractRng = Range(.Cells(1, "A"), .Cells(LstRw, "A"))
'''/// Loop through each contract number in the contract range
For Each ContractNum In ContractRng
'''/// Skip any blank cells
If IsEmpty(ContractNum) Then
GoTo SkipIt
'''/// Skip Duplicate Contract Numbers
If ContractNum.Row <> 1 Then
If ContractNum.Value = ContractNum.Offset(-1).Value Then
GoTo SkipIt
End If

'''/// If the sheet doesn't exit create one
If Not SheetExists(ContractNum.Value) Then
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = ContractNum.Value
End If

Call BadActor_DrawTable_Heading

'''/// Turn off the Autofilter
.AutoFilterMode = False

SkipIt:
Next ContractNum
End With
'''/// Go bank to the original active sheet
Sheets(ThsSht).Select
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
PHP:
Sub CopyContractsDemo()
Dim LstRw As Long, _
    ContractRng As Range, _
    ContractNum As Range, _
    ThsSht As String, _
    Sht As Worksheet, _
    ShtExists As Boolean
    Application.ScreenUpdating = False
    ActiveSheet.AutoFilterMode = False
        
    ThsSht = ActiveSheet.Name
    With Sheets(ThsSht)
        LstRw = .Cells(Rows.Count, "A").End(xlUp).Row
  
        '''/// Set the range of contract numbers
        Set ContractRng = Range(.Cells(1, "A"), .Cells(LstRw, "A"))
        '''/// Loop through each contract number in the contract range
        For Each ContractNum In ContractRng
            '''/// Skip any blank cells
            If IsEmpty(ContractNum) Then
                GoTo SkipIt
            '''/// Skip Duplicate Contract Numbers
                If ContractNum.Row <> 1 Then
                    If ContractNum.Value = ContractNum.Offset(-1).Value Then
                        GoTo SkipIt
                End If
   
            '''/// If the sheet doesn't exit create one
                If Not SheetExists(ContractNum.Value) Then
                    Sheets.Add After:=Sheets(Sheets.Count)
                    ActiveSheet.Name = ContractNum.Value
                End If
    
                Call BadActor_DrawTable_Heading
 
            '''/// Turn off the Autofilter
                .AutoFilterMode = False
  
SkipIt:
        Next ContractNum
    End With
        '''/// Go bank to the original active sheet
        Sheets(ThsSht).Select
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
It's probably because you haven't closed your If statements with an End If. Within the 'For Each ContractNum In Contract Rng' statement, I see 4 If statements, but only 2 'End If' commands.
 
Upvote 0
What you are actually missing is various End Ifs.

The following code will compile but I can't guarantee it'll do what you want because it's hard to follow the code you posted.
Code:
Sub CopyContractsDemo()
Dim LstRw As Long, _
ContractRng As Range, _
ContractNum As Range, _
ThsSht As String, _
Sht As Worksheet, _
ShtExists As Boolean
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False
    ThsSht = ActiveSheet.Name
    With Sheets(ThsSht)
        LstRw = .Cells(Rows.Count, "A").End(xlUp).Row
        
        '''/// Set the range of contract numbers
        Set ContractRng = Range(.Cells(1, "A"), .Cells(LstRw, "A"))
        '''/// Loop through each contract number in the contract range
        For Each ContractNum In ContractRng
            '''/// Skip any blank cells
            If IsEmpty(ContractNum) Then
                GoTo SkipIt
                '''/// Skip Duplicate Contract Numbers
                If ContractNum.Row <> 1 Then
                    If ContractNum.Value = ContractNum.Offset(-1).Value Then
                        GoTo SkipIt
                    End If
                End If
                
                '''/// If the sheet doesn't exit create one
                If Not SheetExists(ContractNum.Value) Then
                    Sheets.Add After:=Sheets(Sheets.Count)
                    ActiveSheet.Name = ContractNum.Value
                End If
                
                Call BadActor_DrawTable_Heading
                
                '''/// Turn off the Autofilter
                .AutoFilterMode = False
            End If
SkipIt:
        Next ContractNum
    End With
    
    '''/// Go bank to the original active sheet
    Sheets(ThsSht).Select
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
You're actually missing a bunch of End If's

I think this is right...

Code:
Sub CopyContractsDemo()
Dim LstRw As Long, _
    ContractRng As Range, _
    ContractNum As Range, _
    ThsSht As String, _
    Sht As Worksheet, _
    ShtExists As Boolean
    Application.ScreenUpdating = False
    ActiveSheet.AutoFilterMode = False
        
    ThsSht = ActiveSheet.Name
    With Sheets(ThsSht)
        LstRw = .Cells(Rows.Count, "A").End(xlUp).Row
  
        '''/// Set the range of contract numbers
        Set ContractRng = Range(.Cells(1, "A"), .Cells(LstRw, "A"))
        '''/// Loop through each contract number in the contract range
        For Each ContractNum In ContractRng
            '''/// Skip any blank cells
            If IsEmpty(ContractNum) Then
                GoTo SkipIt
            End If
            '''/// Skip Duplicate Contract Numbers
            If ContractNum.Row <> 1 Then
                If ContractNum.Value = ContractNum.Offset(-1).Value Then
                    GoTo SkipIt
                End If
            End If
   
            '''/// If the sheet doesn't exit create one
                If Not SheetExists(ContractNum.Value) Then
                    Sheets.Add After:=Sheets(Sheets.Count)
                    ActiveSheet.Name = ContractNum.Value
                End If
    
                Call BadActor_DrawTable_Heading
 
            '''/// Turn off the Autofilter
                .AutoFilterMode = False
  
SkipIt:
        Next ContractNum
    End With
        '''/// Go bank to the original active sheet
        Sheets(ThsSht).Select
        Application.ScreenUpdating = True
End Sub

Hope that helps..
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

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