代码之家  ›  专栏  ›  技术社区  ›  Anu

在excel vba用户窗体中未检测到标签和文本框的对象类型

  •  1
  • Anu  · 技术社区  · 6 年前

    问题

    我有一个功能,它可以探测到一个物体并相应地行动。但是,每当label对象或textbox对象时,它都不会检测label和textbox对象,从而跳过 if 条件。顺便说一下,所有对象都来自用户表单。奇怪的是,它能够检测combobox对象并执行 如果 条件正确

    我的代码

    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)
    

    有人知道为什么吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Storax    6 年前

    在这种情况下,我会使用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