简体   繁体   English

VBA Excel-将数据粘贴到另一列中的数据与列匹配

[英]VBA excel - paste data into another sheet with data in column matches

I need help in vba to insert column of data from sheet 2 , sheet 3 ,sheet 4 into sheet one corresponds to the column header and effective date in column 1 我需要在vba中帮助将工作表2,工作表3,工作表4中的数据列插入到工作表1中,这对应于列标题和第1列中的生效日期

Order date on column B to be paste into sheet 1 with the effective date matches Sheet 1 overview 要在B列上订购的日期粘贴到工作表1中,且生效日期与工作 表1概述 匹配

you could type formulas directly in sheet cells, with the proper use of MATCH() and INDEX() functions 您可以通过正确使用MATCH()和INDEX()函数直接在工作表单元格中键入公式

here's the corresponding VBA code that types in those formulas 这是在这些公式中键入的相应VBA代码

Sub pastebetweensheets1()

With Worksheets("Sheet1").Range("A3", "A" & Worksheets("Sheet1").Rows.Count).SpecialCells(xlCellTypeConstants)
    .Offset(, 1).FormulaR1C1 = "=IF(isnumber(match(RC1,Sheet2!C1,0)),index(Sheet2!C2,match(RC1,Sheet2!C1),0),"""")"
    .Offset(, 2).FormulaR1C1 = "=IF(isnumber(match(RC1,Sheet3!C1,0)),index(Sheet2!C2,match(RC1,Sheet3!C1),0),"""")"
    .Offset(, 3).FormulaR1C1 = "=IF(isnumber(match(RC1,Sheet4!C1,0)),index(Sheet2!C2,match(RC1,Sheet4!C1),0),"""")"
End With
End Sub

where it's assumed that: 假定:

  • "data" sheets names are "Sheet1", "Sheet2" and "Sheet3" “数据”工作表名称为“ Sheet1”,“ Sheet2”和“ Sheet3”

  • "summary" sheet name is "Sheet1" “摘要”工作表名称为“ Sheet1”

  • "data" sheets and "summary" sheet have "structure" as per your examples 根据您的示例,“数据”表和“摘要”表具有“结构”

but of course you can adapt it to whatever names you need 但当然您可以将其修改为所需的任何名称

And there could be an improvement in the way of flexibility: if you typed the name of the "data" sheets in cells "B1:D1" of the "Summary" sheet then the sub would shorten to 灵活性也可能得到改善:如果您在“摘要”表的单元格“ B1:D1”中键入“数据”表的名称,则该子项将缩短为

Sub pastebetweensheets2()
Worksheets("Sheet1").Range("A3", "A" & Worksheets("Sheet1").Rows.Count).SpecialCells(xlCellTypeConstants).Offset(, 1).Resize(, 3).FormulaR1C1 = "=IF(isnumber(match(RC1,indirect(concatenate(""'"",R1C,""'!C1""),False),0)),index(indirect(concatenate(""'"",R1C,""'!C2""),False),match(RC1,indirect(concatenate(""'"",R1C,""'!C1""),false)),0),"""")"
End Sub

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

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