Copy and Paste Problem

kalumbair

New Member
Joined
Aug 2, 2018
Messages
35
Hello Everyone,

i'd like to copy the entire last row of a sheet called ("HL_1") and pasted it to another sheet called ("HL_COMBINED"). I should also point out that they are both table formatted. the code i'm using is below, but it's just not working. any assistance is greatly appreciated.

Private Sub CommandButton1_Click()
Dim i, LastRow
LastRow = Sheets(?HL_1?).Cells(Rows.Count).End(xlUp).Row

For i = 2 To LastRow
If Sheets(?HL_1?).Cells(i, ?A?).Value = ?? Then
Sheets(?HL_1?).Cells(i, ?A?).EntireRow.Copy Destination:=Sheets(?HL_COMBINED?).Range(?A? & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
 
If both sheets have Tables we are working with I always prefer to deal with table names

So are you wanting to copy the last row in Table1 and add it after the lastrow in Table2
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I'm afraid the rocket is still grounded. I tried using table name and got Run-time error '9': subscript out of range. it's this last bit of code that seems to be the problem:
ActiveSheet.ListObjects("HL_COMBINED").Resize Range("$A$1:$C$" & LastRow)
 
Upvote 0
urobee,

the two sheets are ("HL_1") and ("HL_COMBINED"). The tables are named the same. hope that helps and thanks again.
 
Upvote 0
Ok, I see Your new message and You changed the name of the table. it's good!

I colored the table's name with green it's ok if it's the actual name of the table is HL_COMBINED.
ActiveSheet.ListObjects("HL_COMBINED").Resize Range("$A$1:$C$" & LastRow)

The next step:
You need to change the first cell and the last column of the table.
(colored with orange (or something :) )

If your table (with/or without header) on HL_COMBINED sheet is start from A1 cell than it is fine, don't change it. If the table's first cell is somewhere else than change the $A$1 to it.
If the first cell is is A1 than You will need to change only the $C$ part to the actual last column of the table.
For example if the first cell is B2 and the last column is AX than:
ActiveSheet.ListObjects("HL_COMBINED").Resize Range("$B$2:$AX$" & LastRow)
 
Last edited:
Upvote 0
man, this nut doesn't seem to want to crack. I did what you suggested and still no dice. so to make sure I understand, my table has headers and starts with A1 and ends in D so I made the following change. ActiveSheet.ListObjects("HL_COMBINED
").Resize Range("
$A$1:$D$
" & LastRow). is that what you meant?

Ok, I see Your new message and You changed the name of the table. it's good!

I colored the table's name with green it's ok if it's the actual name of the table is HL_COMBINED.
ActiveSheet.ListObjects("HL_COMBINED").Resize Range("$A$1:$C$" & LastRow)

The next step:
You need to change the first cell and the last column of the table.
(colored with orange (or something :) )

If your table (with/or without header) on HL_COMBINED sheet is start from A1 cell than it is fine, don't change it. If the table's first cell is somewhere else than change the $A$1 to it.
If the first cell is is A1 than You will need to change only the $C$ part to the actual last column of the table.
For example if the first cell is B2 and the last column is AX than:
ActiveSheet.ListObjects("HL_COMBINED").Resize Range("$B$2:$AX$" & LastRow)
 
Upvote 0
@kalumbair, if you run the code below what result do you get it the immediate window? (by what result I mean literally copy and paste it in the thread).

Code:
Sub testit()
Debug.Print "< &"; Sheets("HL_COMBINED").ListObjects(1).Name; "& >"
End Sub
 
Upvote 0
Thanks it's a great tip!
I only get the Run-time error '9' error when the table name was not the given "HL_COMBINED".
 
Upvote 0
Yes, Your changes looks good, so the error will be somewhere else.

If You can't use the Inmediate window as MARK858 suggested You can run this code instead (based on MARK858 code, thanks again!):
Just copy and run it from the VBA editor. You will be get a message with the name of the table we (You) need. Copy that name from letter-to-letter.
Code:
Sub testit()
MsgBox Sheets("HL_COMBINED").ListObjects(1).Name
End Sub

BTW the Inmediate window suggestion is a faster way to get info :)

BUT... If You don't want to fight with the names of the tables and the HL_COMBINED sheet has only one table you can change this part:
Code:
ActiveSheet.ListObjects("HL_COMBINED").Resize Range("$A$1:$D$" & LastRow)
to this:
Code:
ActiveSheet.ListObjects(1).Resize Range("$A$1:$D$" & LastRow)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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