宏VBA基于标题复制列并粘贴到另一个工作表

问题描述 投票:0回答:2

背景:这是我第一次处理宏。我将有两个我将要使用的工作表。第一张“源”将提供数据。第二张“最终”将是空白的,并且将是宏将粘贴我希望从“源”表中收集的数据的位置。

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

我必须指定要查找哪些标题的原因是因为“源”表中的标题不会始终位于相同的位置,但“最终”表格的标题始终位于相同的位置 - 所以我可以' T只记录宏在'Source'表中复制A列并粘贴在'Final'表中的A列中。此外,有一天'Source'表可能有170行数据,而另一天它可能有180行。

虽然,最好复制整个列,因为其中一列将有一些空单元而不是现有数据的最后一行。我假设它会在到达所选列中的第一个空单元格时停止复制,这会在列中的空单元格之后省略剩余数据 - 如果我错了,请纠正我。如果复制整个列是最好的方法,那么请将其作为可能解决方案的一部分提供。我附上了一个我希望完成的前后结果的例子:Example of Result

找到Header = X,复制整个列 - >在'Final'表中粘贴到A1中

找到Header = Y,复制整列 - >在'Final'表中粘贴到B1中

等等..

如果我的措辞不准确,我很抱歉 - 我试着尽我所能解释。如果有人可以帮我解决这个问题真是太棒了!谢谢!

excel vba excel-vba copy-paste
2个回答
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

0
投票

你可以试试这个。我认为它清晰而循序渐进。它可以非常优化,但从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

祝好运

© www.soinside.com 2019 - 2024. All rights reserved.