搜索列标题并将标题复制到目标表

时间:2016-04-14 06:20:53

标签: excel vba excel-vba

我需要一个小帮助,标题名称是黄色突出显示的行,现在如果你看到Mastersheet地址在C列,如果新数据出现,那么地址列可能在D列,因此我想要有人帮我调整可以搜索标题名称并复制到目标表的代码,我希望只复制选定的标题而不是整个列或整个标题行,例如只有StudentName,StudentID和要复制的部分目的地表

任何帮助都将不胜感激。

    Sub test5()
    Dim Headers As Variant
    Dim i As Long
    Dim SourceColumn As Range
    Dim DestinationSheet As Worksheet

    Set DestinationSheet = ThisWorkbook.Sheets("destination")
    Headers = Array("StudentName", "StudentID", "Address")

    For i = LBound(Headers) To UBound(Headers)
        With ThisWorkbook.Sheets("Mastersheet").Rows(1)
            Set SourceColumn = .Find(Headers(i), after:=.Cells(1, 1), MatchCase:=False)
        End With

        If Not SourceColumn Is Nothing Then
            Headers.Copy Destination:=DestinationSheet.Range("A4:G3")
        End If
    Next i

End Sub

The yellow highlighted Names are the headers

Destination Sheet

1 个答案:

答案 0 :(得分:0)

试试这个

Sub copyheadernames()

    Sheets("Mastersheet").Range("A1").CurrentRegion.Rows(1).Copy Destination:=Sheets("destination").Range("A1")

End Sub