简体   繁体   English

VBA将符合条件的单元格复制到另一张工作表

[英]VBA copy cells that meet criteria to another sheet

I am brand new to VBA. 我是VBA的新手。 I want to copy a series of cells from Sheet1 to Sheet2 if the cells on Sheet 2 match a value listed in the first column. 如果Sheet 2上的单元格与第一列中列出的值匹配,我想将一系列单元格从Sheet1复制到Sheet2。 For example, I would like to start with SHEET1 and then have the data look like SHEET2 below: 例如,我想从SHEET1开始,然后使数据看起来像下面的SHEET2:

SHEET1
A2=Adam B2=95 C2=92 D2=97 E2=67 F2 blank G2 blank H2 blank
A3=Cindy B3 blank C4 blank D4 blank E4=61 F4=94 G4=95 H4=95

SHEET2
A2=Adam B2=95 C2=92 D2=97 E2=67
A3=Cindy B3=94 C4=95 D4=95 E4=61

In this example, I want to first copy the information from Sheet1 B2:D2 to Sheet2 B2:2. 在此示例中,我想首先将信息从Sheet1 B2:D2复制到Sheet2 B2:2。 Then I would like to copy the information from Sheet1 F3:H3 and fill in the blanks within Sheet 2 B3:D3. 然后,我想复制Sheet1 F3:H3中的信息,并填写Sheet 2 B3:D3中的空白。 All the information in column E should stay where it is. E列中的所有信息都应保留在原处。

Any help would be much appreciated!! 任何帮助将非常感激!!

Edit My (Tony Dallimore) attempt to convert the descriptions of Sheet1 and Sheet2 into something more visual: Edit My(Tony Dallimore)尝试将Sheet1和Sheet2的描述转换为更直观的内容:

Sheet1
   |  A   |  B   |  C   |  D   |  E   |  F   |  G   |  H   |
 2 |Adam  |    95|    92|    97|    67|      |      |      |
 3 |Cindy |      |      |      |      |      |      |      |
 4 |      |      |      |      |    61|    94|    95|    95|

Sheet2
   |  A   |  B   |  C   |  D   |  E   |  F   |  G   |  H   |
 2 |Adam  |    95|    92|    97|    67|      |      |      |
 3 |Cindy |    94|      |      |      |      |      |      |
 4 |      |      |    95|    95|    61|      |      |      |

If I understand your question properly, you probably don't need a VBA solution. 如果我正确理解您的问题,则可能不需要VBA解决方案。 I would just go to columns I:K and put in a formula to pull in either the value from columns B:D or the value from columns F:H, like so: (example for cell I2) 我只需要转到I:K列,然后输入公式即可提取B:D列中的值或F:H列中的值,例如:(单元格I2的示例)

=IF(B2="",F2,B2)

Drag that formula three columns across and down as many rows as you need, and now cells I:K have all the values you need. 将该公式三列拖动到所需的任意数量的行上,现在单元格I:K具有所需的所有值。 You can then Copy>>Paste Special - Paste Values to get everything into columns B:D. 然后,您可以复制>>粘贴特殊-粘贴值,以将所有内容放入B:D列。

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

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