为组合框创建 .additem

Creating .additem for combobox

I have got a list of tariffs that i have set up on an autofilter so that when a specific sales channel is selected and password is correct it shows only the tariffs available to that channel.

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

我下面的 .additem 代码不断返回

"Permission Denied" error

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

如有任何帮助,我们将不胜感激。

首先你需要检查你的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

要在用户窗体控件上循环,请使用如下内容:

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