为组合框创建.additem

时间:2015-11-12 09:50:54

标签: excel vba excel-vba combobox auto-populating

我已经在自动过滤器上设置了关税列表,这样当选择特定的销售渠道并且密码正确时,它只显示该渠道可用的关税。

我的问题是我似乎无法弄清楚如何让命令按钮也填充组合框。

我的.additem代码会一直返回

  

"权限被拒绝"错误

Dim TLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("Tariff Matrix")
Set TLoc = Range("Tariffs")

For Each TLoc In ws.Range("Tariffs")
    With MobilePricing.Tariff1
        .AddItem TLoc.Value
    End With
Next TLoc

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

首先,您需要检查ComboBox的RowSource,如果它不为空,则将其清空。

然后因为你想只有可见的细胞(在自动加载器之后);你需要使用Range("Tariffs").SpecialCells(xlCellTypeVisible)

以下是修改后的代码:

Dim TLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("Tariff Matrix")
Set TLoc = Range("Tariffs")

For Each TLoc In ws.Range("Tariffs").SpecialCells(xlCellTypeVisible).Cells
    With MobilePricing.Tariff1
        .AddItem TLoc.Value
    End With
Next TLoc

要循环使用UserForm控件,请使用以下内容:

Dim Ctrl As Control

For Each Ctrl In Me.Controls
    If TypeName(Ctrl) <> "ComboBox" Then 
    Else
        MsgBox Ctrl.Object.Name
        'Your code for one combobox (everyone will be referenced as Ctrl)
    End If
Next Ctrl
相关问题