【问题标题】:VBA excel - paste data into another sheet with data in column matchesVBA excel - 将数据粘贴到另一张表中,列匹配中的数据
【发布时间】:2016-04-01 07:21:53
【问题描述】:

我在 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

【问题讨论】:

  • 请向我们展示一些代码以及到目前为止您尝试过的内容。所以我们会更容易为您提供帮助。

标签: excel vba


【解决方案1】:

您可以在表格单元格中直接键入公式,并正确使用 MATCH() 和 INDEX() 函数

这是输入这些公式的相应 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

假设:

  • “数据”工作表名称为“Sheet1”、“Sheet2”和“Sheet3”

  • “摘要”工作表名称为“Sheet1”

  • 根据您的示例,“数据”表和“摘要”表具有“结构”

当然,你可以将它改成你需要的任何名字

并且在灵活性方面可能会有所改进:如果您在“摘要”表的单元格“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-28
    • 2018-01-25
    • 1970-01-01
    • 2022-01-24
    • 2015-02-09
    • 2019-07-06
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多