I need to add a Reset button to a userform I am working on in EXCEL VBA. I have the following code that does most of the work, however, I haven't figured out how to clear the refedit controls. This is what I have so far:
===
'Reset Button
Private Sub CommandButton2_Click()
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case "TextBox"
ctrl.Text = ""
Case "ComboBox"
ctrl.ListIndex = -1
Case "OptionButton", "CheckBox"
ctrl.Value = False
Case "ListBox"
For i = 0 To ctrl.ListCount - 1
If ctrl.Selected(i) Then
ctrl.Selected(i) = False
End If
Next i
End Select
Next
End Sub
===
This works for the textboxes, checkboxes, etc. I have tried adding 2 different pieces of code to this sub for clear the refedit controls.
For i = 0 To crtl.RefEdit - 1
ctrl.Selected(i) = vbNullString
Next i
===
Case "RefEdit"
ctrl.RefEdit = vbNullString
===
Neither works as desired. Any suggestions are appreciated!!
Thanks,
Dan
Something like this should work for you:
Dim ctrl As Control
Dim i As Long
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case "TextBox": ctrl.Text = vbNullString
Case "ComboBox": ctrl.ListIndex = -1
Case "OptionButton", "CheckBox": ctrl.Value = False
Case "RefEdit": ctrl.Value = vbNullString
Case "ListBox": For i = 0 To ctrl.ListCount - 1
ctrl.Selected(i) = False
Next i
End Select
Next ctrl
It's indeed hard to know what you need to do to clear controls, because you're working with late-bound calls against a Variant/Object
that are only resolved at run-time, ie the editor can't help you discover the objects' members - you don't get the IntelliSense dropdown when you type that .
dot.
On top of that, TypeName
is making your code vulnerable to typos, and in the extraordinarily unlikely event that you have a 3rd-party ActiveX control library referenced that defines a TextBox
or ListBox
or RefEdit
class/control, you have no way to tell exactly which one you're looking at.
Make type checks with TypeOf...Is
instead, and consider casting the ctrl
control to the appropriate type, so that you keep all the code early-bound, strongly-typed; you get compile-time validation, intellisense and autocomplete all the way - note that becasue of how TypeOf...Is
works, you need to put the conditions in each Case
branch, so you Select Case True
instead:
Private Sub ClearControls()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
Select Case True
Case TypeOf ctrl Is MSForms.TextBox
Dim txtBox As MSForms.TextBox
Set txtBox = ctrl
txtBox.Text = vbNullString
Case TypeOf ctrl Is MSForms.ComboBox
Dim cmbBox As MSForms.ComboBox
Set cmbBox = ctrl
cmbBox.ListIndex = -1
Case TypeOf ctrl Is MSForms.OptionButton
Dim optBtn As MSForms.OptionButton
Set optBtn = ctrl
optBtn.Value = False
Case TypeOf ctrl Is MSForms.CheckBox
Dim chkBox As MSForms.CheckBox
Set chkBox = ctrl
chkBox.Value = False
Case TypeOf ctrl Is MSForms.ListBox
Dim lstBox As MSForms.ListBox
Set lstBox = ctrl
Dim i As Long
For i = 0 To lstBox.ListCount - 1
lstBox.Selected(i) = False
Next
Case TypeOf ctrl Is RefEdit.RefEdit
Dim refEditCtrl As RefEdit.RefEdit
Set refEditCtrl = ctrl
refEditCtrl.Value = vbNullString
End Select
Next
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.