以ComboBox,ListBox和TextBox作为参数清除它们的函数,导致类型不匹配

时间:2019-01-29 13:16:50

标签: excel vba

我是VBA的新手,我无法找到解决方案:我正在尝试编写一个函数来清除ComboBox,ListBox和TextBox,但是我得到了

  

运行时错误13:类型不匹配错误

我不明白为什么。

我正在使用ActiveX控件。

'sub for resetting
 Sub Cancella(testo As TextBox, lista As ListBox, user As ComboBox)

    testo.Text = ""
    lista.Clear
    user.Clear

End Sub


  Sub CommandButtonReset_Click()
'Button locaed in an excelSheet called "Giustificativo"

'InputNumero1 is an textBox in "giustificativo
'ListArticoli1 is a listobx (same sheet)
'ComboBoxUtenti is a comboBox (ame sheet)
Call Cancella(InputNumero1, ListArticoli1, ComboBoxUtenti)

End Sub

我做错了什么,或者根本不可能做?非常感谢你!

1 个答案:

答案 0 :(得分:2)

  1. 请注意,有两种控件。 表单控件 ActiveX控件。因此,您必须确保声明正确的类型,例如MSForms.TextBox
    参见What is the difference between "Form Controls" and "ActiveX Control" in Excel 2010?
    还有VBA MSFORMS vs Controls - whats the difference

  2. 请确保您指定控件位于哪个工作表上,以便Excel不会猜测您指的是哪个工作表。

我建议激活Option Explicit:在VBA编辑器中,转到工具选项 Require Variable Declaration

Option Explicit

Sub Cancella(testo As MSForms.TextBox, lista As MSForms.ListBox, user As MSForms.ComboBox)
    testo.Text = ""
    lista.Clear
    user.Clear
End Sub

Sub CommandButtonReset_Click()
    With Worksheets("Giustificativo")
        Cancella .InputNumero1, .ListArticoli1, .ComboBoxUtenti
    End With
End Sub
相关问题