VBA Next without for complie error

Mr_Smit

New Member
Joined
Jan 11, 2015
Messages
5
Hi there everyone,

I've recently been trying to learn some basic vba to help with duties at my work. I've been following some youtube videos and came into a problem i cant seem to find the answer for. I keep getting a 'next without for' compile error. I know its because i was following youtube videos that weren't using option explicit and i am. So i think it has to do with not defining the variables properly. I've attempted a few different things that i've seen suggested on here, but its still giving me trouble.

here is my code;
Code:
Option Explicit
Private Sub btnAdd_Click()
    
    Dim ws As Worksheet
    Set ws = Worksheets("Overstocks")
    
    Dim i As Variant
    
    Dim newRow As Long
    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
    ws.Cells(newRow, 1).Value = Me.txtBrand.Value
    ws.Cells(newRow, 2).Value = Me.cbCategory.Value
    ws.Cells(newRow, 3).Value = Me.txtQuantity.Value
    
        If opYes.Value = True Then
        ws.Cells(newRow, 4).Value = "Yes"
        Else:
        ws.Cells(newRow, 4).Value = "No"
    End If
    
    Next i

Clear_Form

End Sub

Sub Clear_Form()

For Each Control In Me.Controls
    
    Select Case TypeName(ctrl)
        
        Case "TextBox"
            ctrl.Text = ""
            
        Case "ComboBox"
            ctrl.ListIndex = -1
        Case "OptionButton"
            ctrl.Value = False
                End If
                Next i
        
        End Select


Next

End Sub
Private Sub btnClose_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
    With cbCategory
        .AddItem "Beer"
        .AddItem "Ready To Drink"
        .AddItem "Spirits"
        .AddItem "Wine - Red"
        .AddItem "Wine - Sparkling"
        .AddItem "Wine - White"
    
    End With
    
End Sub

Thanks very much for any help/suggestions people can provide.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,

Where is "for i" in first section of your code? I see only "next i" at the end of it.
 
Upvote 0
...and how are you supposed to be using newRow as currently it looks like you might be using it in the loop when it is defined as the last cell in column A +1 row?

... and what is opYes supposed to be as you haven't defined it?
 
Upvote 0
Hi
As error states, you have a NEXT without a FOR in both btnAdd_Click & an additional Next in the Clear_Form code but in each case, cannot see any need for these.

Try this update to your code & see if helps:

Code:
Option Explicit
Private Sub btnAdd_Click()


    Dim ws As Worksheet
    Dim i As Variant
    Dim newRow As Long


    Set ws = Worksheets("Overstocks")


    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1


    ws.Cells(newRow, 1).Value = Me.txtBrand.Value
    ws.Cells(newRow, 2).Value = Me.cbCategory.Value
    ws.Cells(newRow, 3).Value = Me.txtQuantity.Value
    ws.Cells(newRow, 4).Value = IIf(opYes.Value, "Yes", "No")
    
    Clear_Form
End Sub


Sub Clear_Form()
Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        Select Case TypeName(Ctrl)
        Case "TextBox"
            Ctrl.Text = ""
        Case "ComboBox"
            Ctrl.ListIndex = -1
        Case "OptionButton"
            Ctrl.Value = False
        End Select
    Next Ctrl
End Sub
Private Sub btnClose_Click()
Unload Me
End Sub


Private Sub UserForm_Initialize()
    With cbCategory
        .AddItem "Beer"
        .AddItem "Ready To Drink"
        .AddItem "Spirits"
        .AddItem "Wine - Red"
        .AddItem "Wine - Sparkling"
        .AddItem "Wine - White"
    End With
End Sub

Hope Helpful

Dave
 
Upvote 0
Hi,

Where is "for i" in first section of your code? I see only "next i" at the end of it.

...and how are you supposed to be using newRow as currently it looks like you might be using it in the loop when it is defined as the last cell in column A +1 row?

... and what is opYes supposed to be as you haven't defined it?

This is embarrassing but since i dont even understand the questions i can see i've a)attempted something out of my current skill level and b) Haven't actually UNDERSTOOD the videos i was following. Rather i was just copying. I'll go back and try review. Thanks guys.

Hi
As error states, you have a NEXT without a FOR in both btnAdd_Click & an additional Next in the Clear_Form code but in each case, cannot see any need for these.

Try this update to your code & see if helps:


Dave

You're a legend Dave! That fixed the problem. I'll study how you fixed it and try to learn. Thanks Dave
 
Upvote 0
I've come across another problem that's driving me a bit nuts. I'm getting 'object variable or with block variable not set runtime error 91' when i try to run my macro. I recorded it so i could use a list box to sort through my data.
Code:
Option Explicit

Sub Brand2()
'
' Brand2 Macro
' Sort A-Z for brand

'
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
        Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub Category()
'
' Category Macro
' Sort Category a-z
'

'
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
        Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub Quantity()
'
' Quantity Macro
' Sort Quantity Highest to Lowest
'

'
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
        Range("C1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub IST()
'
' IST Macro
' Sort Z-A
'

'
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
        Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub Date1()
'
' Date1 Macro
' Lowest First
'

'
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort.SortFields.Add Key:= _
        Range("E1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Overstocks").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Thanks for any help people can provide. I wasnt sure if i should start a new thread or try to keep all my newb questions in one place
 
Upvote 0

Forum statistics

Threads
1,213,563
Messages
6,114,332
Members
448,566
Latest member
Nickdozaj

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