Home Requiring Listbox Selection for userform
Reply: 0

Requiring Listbox Selection for userform

user1421 Published in July 21, 2018, 1:47 pm
Private Sub CommandButton1_Click()
whichSheet = ListBox1.Value
Dim n As Integer
n = n + 1
ListBox1.AddItem Sheets(n).Name
Loop Until n = Worksheets.Count

Dim lastrow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
answer = MsgBox("Are you sure you want to add the record?", vbYesNo + vbQuestion, "Add Record")
If answer = vbYes Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox3.Value
Cells(lastrow, 4) = TextBox4.Text
Cells(lastrow, 5) = TextBox5.Text
Cells(lastrow, 6) = TextBox6.Text
    Cells(lastrow, 1) = ""
    Exit Sub
End If

End Sub

Private Sub UserForm_Initialize()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ListBox1.AddItem (ws.Name)
    Next ws
End Sub

Hello guys, I am using the code above which works perfectly for my userform. The only issue I'm having is that when someone doesn't pick a choice from the listbox1 and submits the info, the "Runtime Error" window pops up. I would like to stop that from happening by making a message box appear that tells users to make a choice--->click ok on the message box---> and then resume. If the user doesn't select an option still the same procedure should occur every time. If you have any ideas, I would love to try them out. Thanks.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.568807 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO