简体   繁体   English

使用VBA在一个工作表中创建多个数据验证列表

[英]Create Multiple Data Validation Lists in One Sheet with VBA

Good Afternoon. 下午好。 I am building a database with VBA. 我正在使用VBA构建数据库。 And now I have three data validation drop down menu in one sheet. 现在我在一张表中有三个数据验证下拉菜单。 Each menu has a list that contains item with which I can click on and call out macros. 每个菜单都有一个列表,其中包含我可以单击并调出宏的项目。 And the function of those macros are to print out some item on another worksheet. 而这些宏的功能是在另一个工作表上打印出一些项目。 Here is my code for the data validation. 这是我的数据验证代码。

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
    Select Case Target.Value2
         Case "ABCP"
            Call Macro1
         Case "Accounting Policy"
            Call Macro2
         Case "Audit Committee"
            Call Macro3
         Case "Auto"
            Call Macro4
         Case "Auto Issuer Floorplan"
            Call Macro5
         Case "Auto Issuers"
            Call Macro6
         Case "Board of Director"
            Call Macro7
         Case "Bondholder Communication WG"
            Call Macro8
         Case "Canada"
            Call Macro9
         Case "Canadian Market"
            Call Macro10
         End Select
End If
End Sub

This is the code for the first data validation. 这是第一次数据验证的代码。 I have another two data validation lists that need to have the same functions. 我有另外两个数据验证列表需要具有相同的功能。 However, I cannot assign the cell where the data validation is to another code that is of this format, or the current one will stop working. 但是,我无法将数据验证所在的单元格分配给具有此格式的其他代码,或者当前的代码将停止工作。 I have tried to change the Private_sub Worksheet names, but it won't do. 我试图更改Private_sub工作表名称,但它不会这样做。 How should I do this? 我该怎么做?

Thank you in advance! 先感谢您!

This is not an answer! 这不是答案! But comments do not allow questions this complex, and it will be deleted as soon as clarification is given. 但是评论不允许这么复杂的问题,一旦给出澄清,它将被删除。 (or changed to an answer) (或改为答案)

\n

Right now it is not clear what you want. 现在还不清楚你想要什么。 If the macros to be executed are the same then you can do it like in Nathan_Sav's comment: 如果要执行的宏是相同的,那么你可以像在Nathan_Sav的评论中那样做:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$3" Or Target.Address = "$C$3" Then
    Select Case Target.Value2
    Case "ABCP"
      Call Macro1
    Case "Accounting Policy"
      Call Macro2
    ....

But if they call different macros with the same values then you can do it like this: 但是如果他们使用相同的值调用不同的宏,那么你可以这样做:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$3" Or Target.Address = "$C$3" Then
    Select Case Target.Value2
    Case "ABCP"
      If Target.Address = "$B$3" Then Call Macro1 Else Call Macro101
    Case "Accounting Policy"
      If Target.Address = "$B$3" Then Call Macro2 Else Call Macro102
      ....

And if they are completely different then you can do it like that: 如果它们完全不同,那么你就可以这样做:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$3" Or Target.Address = "$C$3" Then
    Select Case Target.Value2
    Case "ABCP"
      Call Macro1
    Case "Accounting Policy"
      Call Macro2
    ....
    Case "Canadian Market"
      Call Macro10
    Case "ABCP-x"
      Call Macro101
    Case "Accounting Policy-x"
      Call Macro102
    ....
    Case "Canadian Market-x"
      Call Macro110
    End Select

Or split up the 2 cases like this: 或者拆分这两个案例:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$3" Then
    Select Case Target.Value2
    Case "ABCP"
      Call Macro1
    Case "Accounting Policy"
      Call Macro2
    ....
    Case "Canadian Market"
      Call Macro10
    End Select

  ElseIf Target.Address = "$C$3" Then

    Select Case Target.Value2
    Case "ABCP-x"
      Call Macro101
    Case "Accounting Policy-x"
      Call Macro102
    ....
    Case "Canadian Market-x"
      Call Macro110
    End Select

And if they use the same macros but with different values you can do it via Case "ABCP, "some different string" .However, if all validations are needed to evaluate which macro to call, it will change another time. 如果他们使用相同的宏,但使用不同的值,你可以通过Case "ABCP, "some different string"但是,如果需要所有验证来评估要调用哪个宏,它将改变另一个时间。

And if you just need to know which cell was changed in the macro to call you need to hand them over like this: (just an example to show how it works) 如果你只是需要知道在宏中调用了哪个单元格来调用你需要像这样把它们交给:(只是一个例子来说明它是如何工作的)

Private Sub Worksheet_Change(ByVal Target As Range)
  Call TestMacro(Target)
End Sub

Sub TestMacro(rng as Range)
  Debug.Print rng.Address
End Sub

Still some clarification is needed. 还需要一些澄清。 Please help us in helping you 请帮助我们帮助您

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

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