简体   繁体   English

Excel VBA清除用户表单中的所有refedit控件

[英]excel vba clear all refedit controls in a userform

I need to add a Reset button to a userform I am working on in EXCEL VBA. 我需要在我正在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. 我有下面的代码可以完成大部分工作,但是,我还没有弄清楚如何清除refedit控件。 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. 这适用于文本框,复选框等。我尝试将2个不同的代码段添加到此子项以清除refedit控件。

1 : 1:

For i = 0 To crtl.RefEdit - 1
      ctrl.Selected(i) = vbNullString
Next i

=== ===

2: 2:

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 . 确实很难知道您需要做什么来清除控件,因为您正在对仅在运行时解析的Variant/Object进行后期绑定调用,即编辑器无法帮助您发现对象的成员-键入时不会显示IntelliSense下拉列表. 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. 最重要的是, TypeName使您的代码容易受到拼写错误的影响,并且在极不可能的情况下,您引用了定义了TextBoxListBoxRefEdit类/控件的第三方ActiveX控件库,因此您无法准确分辨您正在看哪一个。

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; 而是使用TypeOf...Is类型检查,并考虑将ctrl控件转换为适当的类型,以便使所有代码都早绑定,强类型化。 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: 您将始终获得编译时验证,智能感知和自动完成功能-请注意,由于TypeOf...Is如何工作的,您需要在每个Case分支中放入条件,因此请改为Select Case True

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM