gpt4 book ai didi

excel - 为什么 VBA 中标签会出现类型不匹配错误,而组合框却不会?

转载 作者:行者123 更新时间:2023-12-02 18:45:15 31 4
gpt4 key购买 nike

我正在运行 Excel 2003。我有以下 VBA 代码,对应于一个简单的 UserForm,其中包含一个 ComboBox 控件和一个 Label控制。它通过保存ComboBox 来工作,但在保存Label 时生成类型不匹配 错误。

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 是有限的。但我没想到会出错。]

谢谢大家!

最佳答案

试试这个

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

关于您的查询

It works through the save of the ComboBox but generates a Type mismatch error when saving the Label.

您必须限定 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

关于excel - 为什么 VBA 中标签会出现类型不匹配错误,而组合框却不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12133265/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com