简体   繁体   English

基于多个条件的VBA复制粘贴

[英]VBA Copy Paste based on Multiple Criteria

I'm working on a spreadsheet that has two different drop down variables with 3-4 options in the drop down each. 我正在处理一个电子表格,该电子表格具有两个不同的下拉变量,每个下拉菜单中都有3-4个选项。 IE First drop down has the option to be {2-Tier, 3-Tier, 4-Tier, 40-Tier} and the second can be {2-Tier, 3-Tier, 4-Tier}. IE浏览器的第一个下拉菜单可以选择为{2-Tier,3-Tier,4-Tier,40-Tier},第二个下拉列表可以为{2-Tier,3-Tier,4-Tier}。 I'm very new to VBA, and have only ever worked with SQL before, so I'm kind of shooting in the dark here. 我是VBA的新手,并且以前只使用过SQL,所以在这里我有点黑暗。

Basically, based on the drop down selected, I would like the spreadsheet to "copy and paste" a pricing grid from another corresponding other tab, and paste it on a "set up sheet". 基本上,基于所选的下拉菜单,我希望电子表格从另一个相应的其他选项卡“复制并粘贴”定价网格,然后将其粘贴到“设置表”上。 Since there are two drop downs, I imagine Columns 8-16 being used for the first pricing grid and then 18 down being used for the second set of pricing grids. 由于存在两个下拉列表,因此我想将第8-16列用于第一个定价网格,然后将18列用于第二个定价网格。

Here's what I kind of have so far: 到目前为止,这是我所拥有的:

     Sub Ifs()
    If Worksheets("Set Up Table").Range(B3) = "2-Tier" Then
    Worksheets("2 Tier MEC Rates").Range(A1, F3).Copy Destination:=Worksheets("Set Up Table").Range(A7)

ElseIf If Worksheets("Set Up Table").Range(B3) = "3-Tier" Then
    Worksheets("3 Tier MEC Rates").Range(A1, F4).Copy Destination:=Worksheets("Set Up Table").Range(A7)

ElseIf If Worksheets("Set Up Table").Range(B3) = "4-Tier" Then
    Worksheets("4 Tier MEC Rates").Range(A1, F5).Copy Destination:=Worksheets("Set Up Table").Range(A7)

ElseIf If Worksheets("Set Up Table").Range(B3) = "40-Tier" Then
    Worksheets("7 Tier MEC Rates").Range(A1, F8).Copy Destination:=Worksheets("Set Up Table").Range(A7)

    End If

If Worksheets("Set Up Table").Range(B4) = "2-Tier" Then
    Worksheets("2 Tier LM Rates").Range(A2, E12).Copy Destination:=Worksheets("Set Up Table").Range(A18)

ElseIf Worksheets("Set Up Table").Range(B4) = "3-Tier" Then
    Worksheets("3 Tier LM Rates").Range(A2, E15).Copy Destination:=Worksheets("Set Up Table").Range(A18)

ElseIf Worksheets("Set Up Table").Range(B4) = "4-Tier" Then
    Worksheets("4 Tier LM Rates").Range(A2, E18).Copy Destination:=Worksheets("Set Up Table").Range(A18)

End If

End Sub

My only other fear here is that [other than I'm totally doing this wrong], is that all my pricing tables have formulas based on other variables to determine the correct pricing, and that will somehow get messed up in all of this. 我唯一的担心是(除了我完全没有做错这件事之外),我所有的定价表都有基于其他变量的公式来确定正确的定价,这将以某种方式弄乱所有这些。

Firstly I would take a look at utilising select ... case which you can learn about here: Select...Case Statement (Visual Basic) 首先,我将看一下利用select ... case ,您可以在这里了解到: Select ... Case语句(Visual Basic)

This will tidy up your many many if statements and make it much easier for you to decide what happens "if" a particular option has been selected. 这将整理您的许多if语句,并使您更轻松地决定“如果”选择了特定选项会发生什么。

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

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