TypeOf 对象未检测 excel vba 用户表单中的标签和文本框
TypeOf object not detecting label and textbox in excel vba userforms
问题
我有一个功能,它可以检测到一个对象并做出相应的行为。但是每当标签对象或文本框对象时,它都不会将它们检测为标签和文本框,因此会跳过 if
条件。顺便说一句,所有对象都来自用户窗体。奇怪的是,它能够检测组合框对象并正确执行if
条件
我的代码
Public Function enterObjectsValue(ByVal uiObject As Object)
If TypeOf uiObject Is Label Then
Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Caption
End If
If TypeOf uiObject Is TextBox Or TypeOf uiObject Is ComboBox Then
Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Value
End If
End Function
我调用上面的函数如下所述
Call enterObjectsValue(mainPage.customerGroup)
有人知道为什么吗?
在这种情况下,我会使用 msforms.TextBox 等
Public Function enterObjectsValue(ByVal uiObject As Object)
If TypeOf uiObject Is msforms.Label Then
'Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Caption
Debug.Print uiObject.Caption
End If
If TypeOf uiObject Is msforms.TextBox Or TypeOf uiObject Is msforms.ComboBox Then
'Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Value
Debug.Print uiObject.Value
End If
End Function
问题
我有一个功能,它可以检测到一个对象并做出相应的行为。但是每当标签对象或文本框对象时,它都不会将它们检测为标签和文本框,因此会跳过 if
条件。顺便说一句,所有对象都来自用户窗体。奇怪的是,它能够检测组合框对象并正确执行if
条件
我的代码
Public Function enterObjectsValue(ByVal uiObject As Object)
If TypeOf uiObject Is Label Then
Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Caption
End If
If TypeOf uiObject Is TextBox Or TypeOf uiObject Is ComboBox Then
Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Value
End If
End Function
我调用上面的函数如下所述
Call enterObjectsValue(mainPage.customerGroup)
有人知道为什么吗?
在这种情况下,我会使用 msforms.TextBox 等
Public Function enterObjectsValue(ByVal uiObject As Object)
If TypeOf uiObject Is msforms.Label Then
'Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Caption
Debug.Print uiObject.Caption
End If
If TypeOf uiObject Is msforms.TextBox Or TypeOf uiObject Is msforms.ComboBox Then
'Cells(DeviceSheetLastEmptyCell, headerColumn).Value = uiObject.Value
Debug.Print uiObject.Value
End If
End Function