vba 选择项目时带有宏的组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10529347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:06:45  来源:igfitidea点击:

Combobox with macro on selecting items

excel-vbadrop-down-menuvbaexcel

提问by Kano

I'm very new to VBA and i searched and searched on Google, but can't find an example which deals with my problem.

我对 VBA 很陌生,我在谷歌上搜索和搜索,但找不到解决我的问题的例子。

I got a list of names which I want to put inside a selectable dropdownlist. When i click their name I want to run a different macro i made with their name on. I tried a lot of things yesterday, but everytime it only succed me to assign 1 macro which was called no matter which name i pressed.

我有一个名字列表,我想把它放在一个可选择的下拉列表中。当我点击他们的名字时,我想运行一个我用他们的名字制作的不同的宏。昨天我尝试了很多东西,但每次它只成功分配了 1 个宏,无论我按下哪个名称都会调用它。

I think the solution is pretty simple, but i really got no clue how to do this the most simple way. So hopefully any of you got a link to a simple tutorial or can explain it to me in steps.

我认为解决方案非常简单,但我真的不知道如何以最简单的方式做到这一点。所以希望你们中的任何人都得到了一个简单教程的链接,或者可以分步向我解释它。

Thanks in advance

提前致谢

EDIT: I got 2 names. Birgitte = A:1 Thomas = A:2

编辑:我有两个名字。Birgitte = A:1 托马斯 = A:2

I got a form comboxbox where both names are in. When i press Birgitte i want a macro called BS_Opgave() to run and when i pres Thomas i want Macro TR_Opgave to run.

我有一个表单 comboxbox,其中两个名字都在。当我按下 Birgitte 时,我想要一个名为 BS_Opgave() 的宏运行,当我按下 Thomas 时,我想要运行宏 TR_Opgave。

My problem is I'm not sure how to connect the combox selection to a Macro in the VBA editor. I'm acutyally confused about everything in the editor about comboxing.

我的问题是我不确定如何将组合框选择连接到 VBA 编辑器中的宏。我对编辑器中有关组合的所有内容感到非常困惑。

回答by Siddharth Rout

Paste this code in a module. The Right Click on the Combobox and assign the macro DropDown1_Changeto it :) And you are done.

将此代码粘贴到模块中。右键单击组合框并将宏分配DropDown1_Change给它:) 就完成了。

Option Explicit

Sub DropDown1_Change()
    With ThisWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat
        Select Case .List(.Value)
            Case "Birgitte": BS_Opgave
            Case "Thomas": TR_Opgave
        End Select
    End With
End Sub

Sub BS_Opgave()
    MsgBox "You selected Birgitte"
End Sub

Sub TR_Opgave()
    MsgBox "You selected Thomas"
End Sub

ASSUMPTIONS

假设

I am assuming the following

我假设以下

  • The name of the combobox is "Drop Down 1"
  • The combobox is in "Sheet1"
  • 组合框的名称是“Drop Down 1”
  • 组合框位于“Sheet1”中