简体   繁体   English

在下拉列表中分配宏并使用按钮运行

[英]Assign macro in drop down list and run with button

I have created several macros that generates ID . 我创建了几个生成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. 我的想法是,我将在按钮上方插入一个按钮(我们称其为GENERATE )和一个下拉列表单元格。 As the value changes in the drop down list, the GENERATE button will generate an ID of a different formula accordingly. 随着下拉列表中值的更改, GENERATE按钮将相应地生成其他公式的ID 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. 而不是所有的if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM