简体   繁体   中英

TypeOf object not detecting label and textbox in excel vba userforms

Problem

I have a function whereby it detect an object and behave accordingly. But whenever a label object or textbox object, it is not detecting those as label and textbox and thus skipping the if conditions. By the way all objects are from a userform. The strange thing is, it is able to detect combobox objects and execute if conditions correctly

My Codes

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

I call the above function as stated below

Call enterObjectsValue(mainPage.customerGroup)

Does anyone knows why?

In this case I would use msforms.TextBox etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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