Share via

VBA - Populate table values from another table with button click

Anonymous
2017-04-01T21:56:11+00:00

Hello,

Suppose I have two sheets: Source and Destination. Source sheet has two tables (one below another), Tbl1 and Tbl2. Destination sheet has two tables (one below another), Tbl3 and Tbl4.  

***The tables in Source sheet are DYNAMIC, meaning these tables will have rows added periodically. 

When I click a button, I want the entries in Source.Tbl1 to populate Destination.Tbl3 and Source.Tbl2 to populate Destination.Tbl4. In other words, I want to populate tables with values from other tables when I click a button.

Is there a way I can do this through VBA or some other means?

Any help is much appreciated!

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2017-04-03T06:53:48+00:00

Use:

Dim rng As Range

Dim strTable As String

Dim strAddress As String

Dim i As Long

With Worksheets("Source")

    For i = 1 To .ListObjects.Count

        strTable = .ListObjects(i).Name

        Set rng = .ListObjects(strTable).Range

        strAddress = rng.Cells(1, 1).Address

        rng.Copy Destination:=Worksheets("Destination").Range(strAddress)

        With Worksheets("Destination")

            .ListObjects(i).Name = strTable & "Copy"

        End With

    Next i

End With

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-04-03T06:59:31+00:00

    Hi Doug,

    That's perfect! You saved my entire week - thank you!!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-04-03T06:08:13+00:00

    Hi Doug,

    WOW that definitely makes my life a whole lot easier!! Thank you so much! I do have a follow up question: is there any way I can name each of the destination tables in the VBA code itself? (it's fine if I have to name it one by one too)

    If one table in Source sheet is called "DevTbl" I would like its copy in the Destination sheet to be called "DevTblCopy". Would this be possible?

    Please let me know.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-04-02T19:23:31+00:00

    When I click a button, I want the entries in Source.Tbl1 to populate Destination.Tbl3 and Source.Tbl2 to populate Destination.Tbl4. In other words, I want to populate tables with values from other tables when I click a button.

    Why? There's generally no reason to duplicate perfectly good information. If you're trying to repurpose it for reporting needs, then you might want to look at PivotTables.

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2017-04-02T08:00:37+00:00

    Use the following code:

    Dim rng As Range

    Dim strTable As String

    Dim strAddress As String

    Dim i As Long

    With Worksheets("Source")

        For i = 1 To .ListObjects.Count

            strTable = .ListObjects(i).Name

            Set rng = .ListObjects(strTable).Range

            strAddress = rng.Cells(1, 1).Address

            rng.Copy Destination:=Worksheets("Destination").Range(strAddress)

        Next i

    End With

    Was this answer helpful?

    0 comments No comments