VBA Append Tables - Unexpected result

oddzac

New Member
Joined
Aug 12, 2022
Messages
25
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi all!
Got another newb question for ya.

I'm attempting to grab updated data from a query table and pin it to the bottom of a static table to track year to date trends.

Here's the code I've got:
VBA Code:
Sub TC_Copy_Paste()
    Dim TC As Worksheet
    
    Set TC = Worksheets("TC Data Dump")

    With TC
              
        .Range("P3", ("X3" & .Range("P" & Rows.Count).End(xlUp).Row)).Copy Destination:=TC.Cells(Rows.Count, 5).End(xlUp).Offset(1)
        .Range("AJ3", ("AR3" & .Range("AJ" & Rows.Count).End(xlUp).Row)).Copy Destination:=TC.Cells(Rows.Count, 26).End(xlUp).Offset(1)
        
    End With
End Sub


The code is doing the same thing to 2 separate tables and it runs fine *but* the macro adds an additional 20 blank rows to the bottom of each table.


Ex.
Before:
1676828686057.png



After:

1676828719623.png




Thank you in advance!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If those in P3 and AJ3 are "Tables" then try this version:
VBA Code:
Sub TC_Copy_Paste()
Dim TC As Worksheet, RowNum As Long
'
Set TC = Worksheets("TC Data Dump")

With TC.Range("A2").ListObject
    RowNum = Application.WorksheetFunction.CountA(.ListColumns(1).DataBodyRange)
    .DataBodyRange.Cells(1, 1).Resize(RowNum, 9).Copy Destination:=TC.Cells(Rows.Count, 5).End(xlUp).Offset(1)
End With

With TC.Range("AJ3").ListObject
    RowNum = Application.WorksheetFunction.CountA(.ListColumns(1).DataBodyRange)
    .DataBodyRange.Cells(1, 1).Resize(RowNum, 9).Copy Destination:=TC.Cells(Rows.Count, 26).End(xlUp).Offset(1)
End With
End Sub
 
Upvote 1
Solution
If those in P3 and AJ3 are "Tables" then try this version:
VBA Code:
Sub TC_Copy_Paste()
Dim TC As Worksheet, RowNum As Long
'
Set TC = Worksheets("TC Data Dump")

With TC.Range("A2").ListObject
    RowNum = Application.WorksheetFunction.CountA(.ListColumns(1).DataBodyRange)
    .DataBodyRange.Cells(1, 1).Resize(RowNum, 9).Copy Destination:=TC.Cells(Rows.Count, 5).End(xlUp).Offset(1)
End With

With TC.Range("AJ3").ListObject
    RowNum = Application.WorksheetFunction.CountA(.ListColumns(1).DataBodyRange)
    .DataBodyRange.Cells(1, 1).Resize(RowNum, 9).Copy Destination:=TC.Cells(Rows.Count, 26).End(xlUp).Offset(1)
End With
End Sub
This does the trick! Would it be too much to ask you *how*, though?

I just stepped into VBA over the weekend and I'm trying to learn via these forums and the microsoft site, but it's been a little.. dense.
 
Upvote 0
A "Table" is a ListObject, with certain specific properties and Method.
I used .ListColumns(1).DataBodyRange (ie column 1 of the table "data" range) and WorksheetFunction.CountA to count how many rows are in that table; with this information we can properly determine wich range has to be copied

You might look at this page to learn about Tables: Excel: Working with Tables (VBA)
 
Upvote 1

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top