Compile error: Next without a For statement

zJenkins

Board Regular
Joined
Jun 5, 2015
Messages
148
I'm getting a compile error "Next without a For statement". I got the code from an outside source and was wanting to make it work with what I'm doing, but I can't seem to solve the error. I guess it has to do with the construct of the nested if...then...else...statements.

The procedure is supposed to loop through each of the checkboxes within my listbox, if the checkbox is TRUE, then an email will be sent to the user. If the checkbox is FALSE, then it skips it and goes on to the next checkbox.

Code:
Private Sub CommandButton1_Click()
Dim LastRow As Long
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim curColumn   As Long
Dim ctrl As Control
Dim i As Integer
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem


curColumn = 1
LastRow = Worksheets("sht_data").Cells(Rows.Count, curColumn).End(xlUp).Row


    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" Then
            For i = 7 To LastRow
                If ctrl.Value = True Then

                            Set OutApp = CreateObject("Outlook.Application")
                            Set OutMail = OutApp.CreateItem(0)
                            
                            toList = Cells(i, 6)
                            eSubject = "Hotshop Metrics"
                            eBody = "Please see your attached Hotshop metrics report"
                    
                            On Error Resume Next
                    
                            With OutMail
                                .To = toList
                                .CC = ""
                                .BCC = ""
                                .Subject = eSubject
                                .BodyFormat = olFormatHTML
                                .Display
                                .HTMLBody = eBody & vbCrLf & .HTMLBody
                                '.Send
                            End With
                    
                            On Error GoTo 0
                            Set OutMail = Nothing
                            Set OutApp = Nothing
                  
                Else: Next ctrl
            Next i
        End If
    Next ctrl
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Insert "End If" below "Next ctrl".
 
Upvote 0
Change this
Code:
Else: Next ctrl
to
Code:
End If
 
Upvote 0
Or maybe
Code:
   For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" Then
            If ctrl.Value = True Then
               For i = 7 To Lastrow
                

                            Set OutApp = CreateObject("Outlook.Application")
                            Set OutMail = OutApp.CreateItem(0)
                            
                            toList = Cells(i, 6)
                            eSubject = "Hotshop Metrics"
                            eBody = "Please see your attached Hotshop metrics report"
                    
                            On Error Resume Next
                    
                            With OutMail
                                .To = toList
                                .CC = ""
                                .BCC = ""
                                .Subject = eSubject
                                .BodyFormat = olFormatHTML
                                .Display
                                .HTMLBody = eBody & vbCrLf & .HTMLBody
                                '.Send
                            End With
                    
                            On Error GoTo 0
                            Set OutMail = Nothing
                            Set OutApp = Nothing
                  
               
               Next i
            End If
        End If
    Next ctrl
 
Upvote 0
Or maybe
Code:
   For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" Then
            If ctrl.Value = True Then
               For i = 7 To Lastrow
                

                            Set OutApp = CreateObject("Outlook.Application")
                            Set OutMail = OutApp.CreateItem(0)
                            
                            toList = Cells(i, 6)
                            eSubject = "Hotshop Metrics"
                            eBody = "Please see your attached Hotshop metrics report"
                    
                            On Error Resume Next
                    
                            With OutMail
                                .To = toList
                                .CC = ""
                                .BCC = ""
                                .Subject = eSubject
                                .BodyFormat = olFormatHTML
                                .Display
                                .HTMLBody = eBody & vbCrLf & .HTMLBody
                                '.Send
                            End With
                    
                            On Error GoTo 0
                            Set OutMail = Nothing
                            Set OutApp = Nothing
                  
               
               Next i
            End If
        End If
    Next ctrl


Thanks Fluff! This did the trick

I appreciate the input from Mumps as well.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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