宏VBA,可根据标题复制列并将其粘贴到另一张工作表中

时间:2018-09-06 21:13:45

标签: excel vba excel-vba copy-paste

背景:这是我第一次处理宏。我将使用两个工作表。第一页“来源”将提供可用数据。第二张表“ Final”将为空白,它将是宏将粘贴我希望从“源”表收集的数据的地方。

*我希望宏在“源”表中找到指定的标题,将包含标题的单元格一直复制到现有数据的最后一行(而不是整个列),然后粘贴将其放在指定列(A,B,C等)的“最终”工作表中。 *

我必须指定要查找的标头的原因是因为“源”表中的标头不一定总是在同一位置,而“最终”表的标头总是在同一位置–因此我不能只记录复制“源”表中的A列并粘贴到“最终”表中的A列中的宏。另外,“源”工作表一天可能有170行数据,而另一天可能有180行数据。

尽管如此,最好复制整个列,因为其中一列将有几个空单元格,而不是现有数据的最后一行。我假设它到达选定列中的第一个空单元格时将停止复制,该操作将在列中该空单元格之后保留剩余数据–如果我错了,请纠正我。如果复制整列是最好的方法,那么请提供它作为可能解决方案的一部分。我附上了一个我想完成的前后结果的示例: Example of Result

查找页眉= X,复制整列->粘贴到“最终”工作表的A1中

查找页眉= Y,复制整列->粘贴到“最终”工作表的B1中

等等。

很抱歉,我的措词不正确–我尽力解释了自己的所能。如果有人可以帮我解决这个问题真是太棒了!谢谢!

2 个答案:

答案 0 :(得分:0)

u可以尝试一下。我认为它清晰而分步。它可以非常优化,但是从vba开始,我认为这样更好。

两张表中的列名必须相同。

Sub teste()

Dim val
 searchText = "TEXT TO SEARCH"

 Sheets("sheet1").Select ' origin sheet
 Range("A1").Select
 Range(Selection, Selection.End(xlToRight)).Select
 x = Selection.Columns.Count ' get number of columns

 For i = 1 To x 'iterate trough origin columns
  val = Cells(1, i).Value
    If val = searchText Then
        Cells(1, i).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("sheet2").Select  ' destination sheet
        Range("A1").Select
        Range(Selection, Selection.End(xlToRight)).Select
        y = Selection.Columns.Count ' get number of columns

        For j = 1 To y 'iterate trough destination columns

          If Cells(1, j).Value = searchText Then
            Cells(1, j).Select
            ActiveSheet.Paste
            Exit Sub
          End If

       Next j
    End If
  Next i

End Sub

祝你好运

答案 1 :(得分:0)

我修改了给其他用户的类似问题的答案, 我在大多数数据表中都使用了字典功能,这样我就可以在不破坏代码的情况下移动列,在下面的代码中,您可以移动列,并且仍然可以运行

唯一的主要限制是 1.您的标题名称必须唯一 2.您感兴趣的标题名称必须完全相同。 也就是说,您感兴趣的源标头是PETER,那么您的数据表中的标头应带有PETER,并且标头必须唯一。

Sub RetrieveData()

Dim wb As Workbook
Dim ws_A As Worksheet
Dim ws_B As Worksheet

Dim HeaderRow_A As Long
Dim HeaderLastColumn_A As Long
Dim TableColStart_A As Long
Dim NameList_A As Object
Dim SourceDataStart As Long
Dim SourceLastRow As Long
Dim Source As Variant

Dim i As Long

Dim ws_B_lastCol As Long
Dim NextEntryline As Long
Dim SourceCol_A As Long

Set wb = ActiveWorkbook
Set ws_A = wb.Worksheets("Sheet A")
Set ws_B = wb.Worksheets("Sheet B")
Set NameList_A = CreateObject("Scripting.Dictionary")

With ws_A
    SourceDataStart = 2
    HeaderRow_A = 1  'set the header row in sheet A
    TableColStart_A = 1 'Set start col in sheet A
    HeaderLastColumn_A = .Cells(HeaderRow_A, Columns.Count).End(xlToLeft).Column  'Get number of NAMEs you have

    For i = TableColStart_A To HeaderLastColumn_A
        If Not NameList_A.Exists(UCase(.Cells(HeaderRow_A, i).Value)) Then  'check if the name exists in the dictionary
             NameList_A.Add UCase(.Cells(HeaderRow_A, i).Value), i 'if does not exist record name as KEY and Column number as value in dictionary
        End If
    Next i

End With




With ws_B  'worksheet you want to paste data into
    ws_B_lastCol = .Cells(HeaderRow_A, Columns.Count).End(xlToLeft).Column ' Get number of DATA you have in sheet B
    For i = 1 To ws_B_lastCol   'for each data
        SourceCol_A = NameList_A(UCase(.Cells(1, i).Value))  'get the column where the name is in Sheet A from the dictionaary

        If SourceCol_A <> 0 Then  'if 0 means the name doesnt exists
            SourceLastRow = ws_A.Cells(Rows.Count, SourceCol_A).End(xlUp).Row
            Set Source = ws_A.Range(ws_A.Cells(SourceDataStart, SourceCol_A), ws_A.Cells(SourceLastRow, SourceCol_A))
            NextEntryline = .Cells(Rows.Count, i).End(xlUp).Row + 1 'get the next entry line of the particular name in sheet A

            .Range(.Cells(NextEntryline, i), _
                   .Cells(NextEntryline, i)) _
                   .Resize(Source.Rows.Count, Source.Columns.Count).Cells.Value = Source.Cells.Value
        End If

    Next i
End With


End Sub