vba cast variant listbox / Objects

时间:2017-07-10 09:39:43

标签: vba object casting listbox variant

我遇到了“定义”对象的“转换”问题。

在运行时,我的变量变量是“Variant / Object / Listbox”类型,然后我想将其设置为ListBox变量,将其作为参数路由到另一个需要Listbox对象的函数(GetSelected)。

但我收到错误13:类型与命令“Set lst = v”不兼容。

任何想法如何让它发挥作用?

代码:

Function GetEditableControlsValues(EditableControls As Collection) As Collection
'Gibt die Werte der editierbaren Felder zurück.
Dim v As Variant
Dim coll As New Collection
Dim lst As ListBox

For Each v In EditableControls
    If TypeName(v) = "ListBox" Then
        Set lst = v     'Fehler 13: Typen unverträglich. v zur Laufzeit: Variant/Object/Listbox.
        coll.Add GetCollectionString(GetSelected(lst))
    Else
        coll.Add v.Value
    End If
Next
End Function

2 个答案:

答案 0 :(得分:0)

这是我到目前为止所做的:

想象一下,你有一个包含以下代码的模块:

Option Explicit

Public Sub TestMe()

    Dim colInput        As New Collection
    Dim colResult       As Collection
    Dim lngCount        As Long
    Dim ufMyUf          As UserForm

    Set ufMyUf = UserForm1

    Set colInput = GetListBoxObjects(ufMyUf)

    For lngCount = 1 To colInput.Count
        Debug.Print colInput(lngCount).Name
    Next lngCount

End Sub

Function GetListBoxObjects(uf As UserForm) As Collection

    Dim colResult   As New Collection
    Dim objObj      As Object
    Dim ctrCont     As Control

    For Each ctrCont In uf.Controls
        If LCase(Left(ctrCont.Name, 7)) = "listbox" Then
            Set objObj = ctrCont
            colResult.Add objObj
        End If
    Next ctrCont

    Set GetListBoxObjects = colResult

End Function

如果您运行TestMe,您将获得ListBox个对象的集合。无论如何,我不确定你如何将它们传递给集合函数,因此我决定迭代UserForm,从而检查它上面的所有对象。

干杯!

答案 1 :(得分:0)

我自己在转换控件时遇到了问题,没有找到可以轻松使用的通用解决方案。

最终,我找到了一种实现方法:将其存储为“对象”可以轻松地转换为控件的实际类型。

我已经测试(并使用)了

下面的子代码显示它的工作原理(这里:工作表上的1个TextBox; 1个ListBox; 1个ComboBox; 1个CommandButton)

Sub Test_Casting()
    Dim lis As MSForms.ListBox
    Dim txt As MSForms.TextBox
    Dim btn As MSForms.CommandButton
    Dim com As MSForms.ComboBox

    Dim numObjects As Integer: numObjects = Me.OLEObjects.Count
    Dim obj() As Object
    ReDim obj(1 To numObjects) As Object

    Dim i As Integer: i = 0

    Dim cttl As OLEObject
    For Each ctrl In Me.OLEObjects
        i = i + 1
        Set obj(i) = ctrl.Object
    Next ctrl

    Dim result As String
    For i = 1 To numObjects
        If TypeOf obj(i) Is MSForms.ListBox Then
            Set lis = obj(i): result = lis.Name
        ElseIf TypeOf obj(i) Is MSForms.TextBox Then
            Set txt = obj(i): result = txt.Name
        ElseIf TypeOf obj(i) Is MSForms.CommandButton Then
            Set btn = obj(i): result = btn.Name
        ElseIf TypeOf obj(i) Is MSForms.ComboBox Then
            Set ComboBox = obj(i): result = com.Name
        Else
            result = ""
        End If
        If (Not (result = "")) Then Debug.Print TypeName(obj(i)) & "  name= " & result
    Next i

    For i = 1 To numObjects
        Set lis = IsListBox(obj(i))
        Set txt = IsTextBox(obj(i))
        Set btn = IsCommandButton(obj(i))
        Set com = IsComboBox(obj(i))
        result = ""
        If (Not (lis Is Nothing)) Then
            result = "ListBox " & lis.Name
        ElseIf (Not (txt Is Nothing)) Then
            result = "TexttBox " & txt.Name
        ElseIf (Not (btn Is Nothing)) Then
            result = "CommandButton " & btn.Name
        ElseIf (Not (com Is Nothing)) Then
            result = "ComboBox " & com.Name
        End If
        Debug.Print result
    Next i
End Sub

Function IsListBox(obj As Object) As MSForms.ListBox
    Set IsListBox = IIf(TypeOf obj Is MSForms.ListBox, obj, Nothing)
End Function

Function IsTextBox(obj As Object) As MSForms.TextBox
    Set IsTextBox = IIf(TypeOf obj Is MSForms.TextBox, obj, Nothing)
End Function

Function IsComboBox(obj As Object) As MSForms.ComboBox
    Set IsComboBox = IIf(TypeOf obj Is MSForms.ComboBox, obj, Nothing)
End Function

Function IsCommandButton(obj As Object) As MSForms.CommandButton
    Set IsCommandButton = IIf(TypeOf obj Is MSForms.CommandButton, obj, Nothing)
End Function

它的一个用途是在一个类中处理事件的类。

Private WithEvents intEvents As IntBoxEvents
Private WithEvents decEvents As DecBoxEvents

Private genEvents As Object
Private genControl as OLEobject

Public sub Delegate(ctrl As OLEObject)
    set genControl = ctrl 
    ' Code for creating intEvents or decEvents 
    if .... create intevents.... then set genEvents = new IntEvents ' pseudo code
    if .... create decevents.... then set genEvents = new DecEvents ' pseudo code
end sub

我希望这可以帮助其他在投射控件方面遇到困难的人