在下拉列表中指定宏和按键运行 [英] Assign macro in drop down list and run with button

查看:521
本文介绍了在下拉列表中指定宏和按键运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了几个宏生成 ID 。现在我需要把它们在下拉列表中选择。

的想法是,我会插入一个按钮(姑且称之为生成)和一个下拉按钮上方列表1细胞。正如在下拉列表中值的变化,生成按钮将相应产生不同配方的 ID。这里是我的codeS虽然我不知道他们是对的。

 子GenerateID()
昏暗宏名作为字符串
宏名=范围(A4)。价值如果宏名=身份证然后
    Application.Run(GenerateNRICFIN)
elseif的宏名= FIN然后
    Application.Run(GenerateFIN)
elseif的宏名= RB然后
    Application.Run(GenerateRB)
elseif的宏名= RB2然后
    Application.Run(GenerateRB2)
elseif的宏名= RB3然后
    Application.Run(GenerateRB3)
elseif的宏名= RB4然后
    Application.Run(GenerateRB4)
elseif的宏名= RB5然后
    Application.Run(GenerateRB5)
elseif的宏名= RC然后
    Application.Run(GenerateRC)
elseif的宏名= RC2然后
    Application.Run(GenerateRC2)
elseif的宏名= RC3然后
    Application.Run(GenerateRC3)
elseif的宏名= RC4然后
    Application.Run(GenerateRC4)
elseif的宏名= RC5然后
    Application.Run(GenerateRC5)
elseif的宏名= RC6然后
    Application.Run(GenerateRC6)
万一
结束小组


解决方案

使用这样的:

 子GenerateID()
昏暗宏名作为字符串
宏名=范围(A4)。价值
宏名= ThisWorkbook.Sheets(工作表Sheet1)。范围(A4)。价值IF(宏名=身份证),那么宏名=NRICFINApplication.Run生成&放大器;宏名END子

而不是所有的if语句。

I have created several macros that generates ID. Now I need to put them in a drop down list to be selected.

The idea is that I'll insert a button (let's call it GENERATE) and a drop down list 1 cell above the button. As the value changes in the drop down list, the GENERATE button will generate an ID of a different formula accordingly. Here is my codes although I'm not sure if they are right..

Sub GenerateID()
Dim macroName As String
macroName = Range("A4").Value

If macroName = NRIC Then
    Application.Run (GenerateNRICFIN)
ElseIf macroName = FIN Then
    Application.Run (GenerateFIN)
ElseIf macroName = RB Then
    Application.Run (GenerateRB)
ElseIf macroName = RB2 Then
    Application.Run (GenerateRB2)
ElseIf macroName = RB3 Then
    Application.Run (GenerateRB3)
ElseIf macroName = RB4 Then
    Application.Run (GenerateRB4)
ElseIf macroName = RB5 Then
    Application.Run (GenerateRB5)
ElseIf macroName = RC Then
    Application.Run (GenerateRC)
ElseIf macroName = RC2 Then
    Application.Run (GenerateRC2)
ElseIf macroName = RC3 Then
    Application.Run (GenerateRC3)
ElseIf macroName = RC4 Then
    Application.Run (GenerateRC4)
ElseIf macroName = RC5 Then
    Application.Run (GenerateRC5)
ElseIf macroName = RC6 Then
    Application.Run (GenerateRC6)
End If
End Sub

解决方案

Use this:

Sub GenerateID()
Dim macroName As String
'macroName = Range("A4").Value 
macroName = ThisWorkbook.Sheets("Sheet1").Range("A4").Value

IF (macroName = "NRIC") then macroName = "NRICFIN"

Application.Run "Generate" & macroName

END Sub

Instead of all your if-statements.

这篇关于在下拉列表中指定宏和按键运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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