根据更改的组合框值设置单元格查找值



我在Excel工作表中输入了一个combobox。我希望它起作用,以便无权访问VBA的用户可以从dropdown中选择一个值,然后另一个单元格中的值将对该值执行vlookup

首先,我插入了一个框,并尝试基于此设置单元格值。

Sub InsertComboBox()
#inserts dropdown box on the front page and sets the values as the list of DMA from the pipe_totals sheet
#this should be the most complete list so will not change dependant on asset
Dim arearange As Range
Set arearange = Sheets("pipe_totals").Range("a:a")
lastrowi = Application.WorksheetFunction.CountA(arearange)
Sheets("front page").Activate
With Range("f5:g5")
Set Combo = ActiveSheet.DropDowns.Add(.Left, .Top, .Width, .Height)
End With
Combo.List() = Sheets("pipe_totals").Range("A2:A" & lastrowi).Value
.Range("k9").Value = Combo.Value 'only works on current combobox value which is 0
End Sub

有没有办法设置它,使vlookup根据用户的选择是动态的?

在此示例中,只需设置正确的组合名称。它应该没问题,前提是您的组合框列出了上面提到的"Range("A2:A"和lastrowi)"中的值。

Sub "comboname"_Change()
    Dim list_val As Long
    list_val = Worksheets("front page").Shapes("comboname").ControlFormat.Value
    Range("K9") = Worksheets("pipe_totals").Cells((list_val + 1), 1)
End Sub
Sub test()
    Dim z As Shape
        For Each z In Worksheets("front page").Shapes
            Debug.Print z.Name
        Next z
End Sub

据我了解,您希望每次组合框值更改时,单元格 K9 也将具有相同的值。是吗?如果是这种情况,请右键单击组合框并选择"分配宏"。然后选择"创建"。然后在创建的子内部,它应该看起来像这样:

Sub "comboname"_Change()
End Sub

您还应该粘贴最终的代码行。

.Range("k9").Value = Combo.Value

这样做意味着您希望每次组合框值更改时都执行该行代码。

最新更新