Search code examples
excelvbauserform

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?


Solution

  • 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