Consulting

Results 1 to 3 of 3

Thread: Copy Column Table x times and add counter

  1. #1
    VBAX Regular
    Joined
    Oct 2017
    Posts
    14
    Location

    Copy Column Table x times and add counter

    Hi there,


    I am trying create a VBA code to copy My Product list (Table1) to my Master table (Table2).
    I would like to repeat the process 12 times and add a "Month no" in the adjacent column.

    Seems like an easy task but somehow I am going nowhere with this.

    Copy_x_Times.JPG

    Can anybody please help me?

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i assume the second table has Header Row only, ie, an empty table.

    try:
    Sub vbax_61109_copy_tbl_to_tbl_add_counter()
      
        Dim tblCopy As ListObject, tblPaste As ListObject
        Dim i As Long, Num As Long, PasteRow As Long
        Dim tblData
        
        Set tblCopy = Worksheets("Sheet1").ListObjects("Table1")
        Set tblPaste = Worksheets("Sheet1").ListObjects("Table2")
        'change sheet name and table names to suit
        
        With tblCopy
            tblData = .ListColumns(1).DataBodyRange.Value
            Num = .ListColumns(1).DataBodyRange.Count
        End With
        
        With tblPaste
            .Resize .Range.Resize(Num * 12, .Range.Columns.Count)
            For i = 1 To 12
                PasteRow = Num * (i - 1) + 1
                .ListColumns(1).DataBodyRange.Cells(PasteRow).Resize(Num, 1).Value = tblData
                .ListColumns(2).DataBodyRange.Cells(PasteRow).Resize(Num, 1).Value = i
            Next i
        End With
            
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Oct 2017
    Posts
    14
    Location

    Smile

    Quote Originally Posted by mancubus View Post
    i assume the second table has Header Row only, ie, an empty table.

    try:
    Sub vbax_61109_copy_tbl_to_tbl_add_counter()
      
        Dim tblCopy As ListObject, tblPaste As ListObject
        Dim i As Long, Num As Long, PasteRow As Long
        Dim tblData
        
        Set tblCopy = Worksheets("Sheet1").ListObjects("Table1")
        Set tblPaste = Worksheets("Sheet1").ListObjects("Table2")
        'change sheet name and table names to suit
        
        With tblCopy
            tblData = .ListColumns(1).DataBodyRange.Value
            Num = .ListColumns(1).DataBodyRange.Count
        End With
        
        With tblPaste
            .Resize .Range.Resize(Num * 12, .Range.Columns.Count)
            For i = 1 To 12
                PasteRow = Num * (i - 1) + 1
                .ListColumns(1).DataBodyRange.Cells(PasteRow).Resize(Num, 1).Value = tblData
                .ListColumns(2).DataBodyRange.Cells(PasteRow).Resize(Num, 1).Value = i
            Next i
        End With
            
    End Sub
    Wow, that was quick
    This is brilliant, exactly what I needed. Thank you mancubus

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •