Search code examples
excelvbarangeexcel-tableslistobject

How do I copy a Table column and paste it into another table column on another sheet?


I have two worksheets. I enter weekly numbers into a column on one sheet and at the end of the week I copy those numbers to a table column on the other sheet. I clear the contents and start with new numbers in same column. I would like a macro to do this. Simply recording a macro has not worked.

enter image description here

enter image description here

I also have a reference in cell "F2" that I would like to use that names the column in the table I want to copy too. Thanks.

This is what I tried

  Range("TM[Net]").Select
    Selection.Copy
    Sheets("C").Select
    Range("D2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, 
Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("H9").Select
    Sheets("M").Select
    Range("G5").Select
    Application.CutCopyMode = False
    Sheets("C").Select
End Sub

Solution

  • Copy Excel Table Data Column

    enter image description here enter image description here enter image description here enter image description here

    Sub CopyColumn()
        
        ' Workbook
        
        Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
            
        ' Source
        
        Dim sws As Worksheet: Set sws = wb.Sheets("M")
        
        Dim sHeader As String: sHeader = sws.Range("G2").Value
        If Len(sHeader) = 0 Then Exit Sub
        
        Dim slo As ListObject: Set slo = sws.ListObjects("TM")
        Dim scrg As Range: Set scrg = slo.ListColumns(sHeader).DataBodyRange
        
        ' Destination
        
        Dim dws As Worksheet: Set dws = wb.Sheets("C")
        Dim dHeader As String: dHeader = sws.Range("G3").Value
        If Len(sHeader) = 0 Then Exit Sub
        
        Dim dlo As ListObject: Set dlo = dws.ListObjects("TC")
        Dim dfCell As Range:
        Set dfCell = dlo.ListColumns(dHeader).Range.Cells(1).Offset(1)
        Dim dcrg As Range: Set dcrg = dfCell.Resize(scrg.Rows.Count)
        
        ' Copy values.
        
        dcrg.Value = scrg.Value
        
        ' Inform.
        MsgBox "Copied from ""M!" & scrg.Address(0, 0) & """ to ""C!" _
            & dcrg.Address(0, 0) & """.", vbInformation
        
    End Sub