根据更改Combobox值设置单元格查找值

我在Excel工作表中input了一个combobox 。 我希望它能够工作,以便无权访问VBA用户可以从dropdownselect一个值,然后另一个单元格中的值将对此值执行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是dynamic的,取决于用户的select?

在这个例子中,只需设置正确的组合名称。 如果你的combobox列出了范围(“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 

据我所知,你想要每次combobox的值改变,单元格K9也会有相同的值。 是对的吗? 如果是这种情况,那么右键单击combobox并select“分配macros”。 然后select“创build”。 然后里面的子创build,应该看起来像这样:

 Sub "comboname"_Change() End Sub 

你也应该粘贴最后的代码行。

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

这样做意味着每当combobox值更改时都要执行该行代码。