【问题标题】:Copy specific column data USING BUTTON from Book1 to Book2 based on column header matching根据列标题匹配将特定列数据 USING BUTTON 从 Book1 复制到 Book2
【发布时间】:2016-03-06 09:43:18
【问题描述】:

在 Excel 中,我想根据列标题匹配将特定列数据从 Book1 复制到 Book2。 Book1 和 Book2 具有相同的标题。我需要 Book1 的 sheet2 中的单个按钮来执行此复制宏或 VBscript。

另外,在 Book2 中,我有一些列在 row1 中有数据。此数据应根据从 Book1 复制的数据行数自动填充到所有行中(SAME DATA)。

【问题讨论】:

  • 请阅读How to Ask 以获得本网站最有效的帮助。

标签: excel macros vba


【解决方案1】:

我尝试了本网站中的以下代码。它工作正常。现在我需要问题的第二部分。

Sub CopyHeaders()
Dim header As Range, headers As Range
Set headers = Workbooks("Book1.xlsm").Worksheets("sheet1").Range("A1:Z1")

For Each header In headers
    If GetHeaderColumn(header.Value) > 0 Then
        Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Workbooks("Book2.xlsm").Worksheets("sheet1").Cells(2, GetHeaderColumn(header.Value))
    End If
Next
End Sub

Function GetHeaderColumn(header As String) As Integer
   Dim headers As Range
   Set headers = Workbooks("Book2.xlsm").Worksheets("sheet1").Range("A1:Z1")
   GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多