Listbox with Check Box Option??

T

TotallyConfused

Hi, I am trying to clear out all checkboxes, textboxes and listboxes with a
"cancel" button. With the help of this forum, I have the code below which
works great except for a couple of listboxes that have checkboxes (1-
fmliststyleoption). These two listboxes do not clear when I click on the
cancel button. Can someone please help me with this? Thank you.

Dim cCont As Control
If MsgBox("Do you really want to cancel?", vbYesNo, "Cancel Action") =
vbYes Then
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
cCont.Value = False
End If
If TypeName(cCont) = "TextBox" Then
cCont.Value = ""
End If
If TypeName(cCont) = "Listbox" Then
cCont.Value = ""
End If
Next cCont
End If
Me.txtFormDate = Format(Date, "mm/dd/yyyy")
Exit Sub

End Sub
 
D

Dave Peterson

Watch your typing. String comparisons are case sensitive.

If TypeName(cCont) = "ListBox" Then
cCont.ListIndex = -1
End If

ListBox with a capital B.

And I changed the the method for deselecting any items from the listbox.

I like this syntax to stop my typos:

If TypeOf cCont Is msforms.CheckBox Then
cCont.Value = False
End If
If TypeOf cCont Is msforms.TextBox Then
cCont.Value = ""
End If
If TypeOf cCont Is msforms.ListBox Then
cCont.Value = ""
End If

It has the added benefit of being able to distinguis between textboxes on
worksheets--are they from the Drawing toolbar or from the control toolbox
toolbar.
 
T

TotallyConfused

Thank you Dave for your response. However, the ListBox checkboxes will not
clear with the code provided. I copied your code exactly and everything
else works fine except this listbox where I have it set to List style option
1. This option has checkboxes that will not uncheck when I click on the
cancel button. Can you please help? Thank you.
 
T

TotallyConfused

Thank you for responding. There are over 350 lines I would have to go
through if I follow your code. This is what I have now and it seems to work.
However, I am new to code in Excel and would like your opinion. Thank you
very much .

Dim cCont As Control
If MsgBox("Do you really want to cancel?", vbYesNo, "Cancel Action") =
vbYes Then
For Each cCont In Me.Controls
If TypeOf cCont Is MSForms.CheckBox Then
cCont.Value = False
End If
If TypeOf cCont Is MSForms.TextBox Then
cCont.Value = ""
End If
If TypeOf cCont Is MSForms.ListBox Then
cCont.Value = ""
End If
If TypeName(cCont) = "ListBox" Then
cCont.ListStyle = 0
End If
Next cCont
End If
Me.ListBox4.ListStyle = 1
Me.ListBox5.ListStyle = 1
Me.txtFormDate = Format(Date, "mm/dd/yyyy")
Exit Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top