ListBox error

zJenkins

Board Regular
Joined
Jun 5, 2015
Messages
148
Hi,

On my userform I have a ListBox that is auto generated based on a list on my main worksheet. From the list on the userform the user can select which people they want the file to be sent to. The code I wrote loops through each of the users in the ListBox to see which ones are selected. If they are selected than an email is sent. The code always works until it gets to the last name on the ListBox. Once it gets to the last name I get a "run-time error - Could not get the Selected property. Invalid argument.

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 i = 7 To LastRow
            If ListBox1.Selected(i - 6) = 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
            End If
        Next i
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello zJenkins,

Both the ListBox and the ComboBox indices start at zero, not one.

Code:
If ListBox1.Selected(i - 7) = True Then
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,560
Latest member
Torchwood72

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