0

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

下面是我当前尝试的代码:

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

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 我的代码在给我的那一行失败了

运行时错误 438 '对象不支持此属性或方法。

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

4

1 回答 1

1

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

' 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
于 2019-05-15T13:55:51.520 回答