将宏分配给组合框 [英] Assigning macros to a combobox

查看:42
本文介绍了将宏分配给组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为下拉菜单分配3个宏.

I have been trying to assign 3 macros to a dropdown menu.

我使用开发人员">插入">组合框"(不是ActiveX控件)创建了一个组合框,其中包含三个选项:表1",表2"和表3".现在,我想为这三个选项中的每一个分配一个宏.如果单击表1",则将运行 macro1 ;如果是表2",则 macro2 等.

I created a combobox using Developer > Insert > ComboBox (not ActiveX controls) with three options: "Table 1", "Table 2" and "Table 3". Now, I want to assign a macro to each of those three options. If "Table 1" is clicked, then macro1 would run; if "Table 2 " then macro2, etc.

我能够通过右键单击>格式控制,然后选择输入范围来创建菜单.

I was able to create a menu through right clicking > Format Control and then choosing an input range.

但是如何将下拉选项链接到宏?

But how do I link the dropdown options to macros?

推荐答案

您要分配宏到组合框.

  1. 打开一个模块并编写一个宏,例如 DropDown1_Change().
  2. 右键单击组合框>分配宏...在列表中选择您的宏>确定.

分配的宏应确定单击了哪个下拉选项,然后根据该选择调用另一个宏.看起来可能像这样:

The assigned macro should determine which dropdown option was clicked, and call a different macro based on that choice. It could look something like this:

Sub DropDown1_Change()
    Dim c As ControlFormat
    Set c = Sheet1.Shapes("Drop Down 1").ControlFormat ' or whatever yours is called

    'Choose which macro to run based on the selected value in the combobox
    Select Case c.Value
    Case 1: Macro1
    Case 2: Macro2
    Case 3: Macro3
    End Select
End Sub

Sub Macro1()
    MsgBox "Macro 1"
End Sub
Sub Macro2()
    MsgBox "Macro 2"
End Sub
Sub Macro3()
    MsgBox "Macro 3"
End Sub

这篇关于将宏分配给组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆