3

我正在运行 Excel 2003。我有以下 VBA 代码,对应于一个由控件和控件UserForm组成的简单代码。它通过保存工作,但在保存时会产生类型不匹配错误。ComboBoxLabelComboBoxLabel

Private whatComboBox As ComboBox
Private whatLabel As Label

Private Sub UserForm_Initialize()
    Call SaveComboBox(SomeComboBox)
    Call SaveLabel(SomeLabel)
End Sub

Private Sub SaveComboBox(aComboBox As ComboBox)
    Set whatComboBox = aComboBox
End Sub

Private Sub SaveLabel(aLabel As Label)
    Set whatLabel = aLabel
End Sub

[而且我很欣赏 VBA 与大多数其他语言相比是有限的。但我不会想到这是会出错的那种事情。]

谢谢大家!

4

1 回答 1

3

试试这个

Private whatComboBox As msforms.ComboBox
Private whatLabel As msforms.Label

Private Sub UserForm_Initialize()
    Call SaveComboBox(SomeComboBox)
    Call SaveLabel(SomeLabel)
End Sub

Private Sub SaveComboBox(aComboBox As msforms.ComboBox)
    Set whatComboBox = aComboBox
End Sub

Private Sub SaveLabel(aLabel As msforms.Label)
    Set whatLabel = aLabel
End Sub

关于您的查询

它通过保存组合框起作用,但在保存标签时会生成类型不匹配错误。

您必须限定 Label 对象,因为 Excel 也有一个名为 Label 的对象

选择

Private whatComboBox As Control
Private whatLabel As Control

Private Sub UserForm_Initialize()
    Call SaveComboBox(SomeComboBox)
    Call SaveLabel(SomeLabel)
End Sub

Private Sub SaveComboBox(aComboBox As Control)
    Set whatComboBox = aComboBox
End Sub

Private Sub SaveLabel(aLabel As Control)
    Set whatLabel = aLabel
End Sub
于 2012-08-26T20:31:05.673 回答