使用不同的值填充 ActiveX 组合框

Populating ActiveX comboboxes with distinct values

我的问题是我想用特定名称填充 ActiveX 控件组合框List_Funds 因为我重命名了它。总体基于我在名为 Table_Funds 的工作簿中生成的 table 中的特定列。我希望组合框仅填充 table 的唯一值。当我打开工作簿时,代码应该 运行。

我当前尝试的代码如下:

下面的代码位于包含我所有声明的指定模块中

Option Explicit
Option Base 0

' This module contains all constants and variable declarations
' **** Declarations ****

' Worksheets and workbooks
Public ws             As Worksheet
Public ws_O           As Worksheet
Public ws_S           As Worksheet
Public wkb            As Workbook

' Integers
Public i              As Integer
Public j              As Integer

' Variants, objects and ranges
Public Data           As Variant
Public Funds_List     As Object
Public rng            As Range
Public tbl            As ListObject

Sub Fixed()

    Set wkb = ThisWorkbook
    Set ws_O = wkb.Sheets("Overview")
    Set ws_S = wkb.Sheets("SQL")
    Set Funds_List = ws_O.OLEObjects("List_Funds").Object
    Set tbl = ws_O.ListObjects("Table_Funds")

End Sub

以下代码在 ThisWorkbook 模块中

Option Explicit

Private Sub Workbook_Open()
' Computing when opening workbook

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Call modCnt.Fixed

    ' Populating table
    Data = modGlobal.GetSql(modGlobal.Compose_sSql(1))
    tbl.DataBodyRange.ClearContents
    For i = LBound(Data, 2) To UBound(Data, 2)
        For j = LBound(Data, 1) To UBound(Data, 1)
            tbl.DataBodyRange(1 + i, 1 + j) = Data(j, i)
        Next j
    Next i

    ' Populating combobox
    With Funds_List
        For Each rng In tbl.ListColumns("Name").DataBodyRange
            If Not .exists(rng.Value) Then ' < ---- code fails here
                .AddItem rng.Value
            End If
        Next rng
    End With


    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

我的代码在 If Not .exists(rng.Value) Then 行失败 给我

run-time error 438 'Object does not support this property or method.'

table 正在填充(即您可以 忽略填充 table 的部分,因为它调用了不同模块中的子程序) 并查看代码,我知道 rng 具有正确的值(我的 table 的 databodyrange 中的第一个值)。

可能有人创建了函数。尝试替换此块:

' Populating combobox
Dim Exists As Boolean
Dim t As Long

Exists = False

With Funds_List
    For Each Rng In tbl.ListColumns("Name").DataBodyRange
        For t = 1 To .ListCount - 1
            If Rng.Value = CStr(.List(t)) Then
                Exists = True
                Exit For
            End If
        Next t
        If Exists = False Then
            .AddItem Rng.Value
        End If
    Next Rng
End With