Copy Last column to Last row

Vannes

New Member
Joined
Oct 29, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am currently working on copying the data from excel table to paste in Outlook email, but my main problem is I wanted to copy the table dynamically because my last data in the table is dependent on what it needs to capture (For example the last column may be until to H, or until to K, it depends on the data captured, as well as the last row)

I am not pro or good in VBA, so Im not sure if Im doing this right. Hope you can help

So my code is this:

Sub CopyTable

Dim lastrow as integer
Dim lastcol as integer
Dim ws as worksheet

set ws = Thisworkbook.Sheets("Sheet1")

lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
lastcol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column

ws.Range(lastcol & lastrow).Copy
End Sub

and my First column is no blank. So the example table can be like this:

1698570310958.png
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try changing
VBA Code:
ws.Range(lastcol & lastrow).Copy
To
VBA Code:
ws.Cells(lastrow, lastcol).Copy
If it is just the last cell you require

Or if there it is the full column then try

VBA Code:
Range(ws.Cells(1, lastcolumn), ws.Cells(lastrow, lastcolumn)).Copy
 
Last edited:
Upvote 0
Try changing
VBA Code:
ws.Range(lastcol & lastrow).Copy
To
VBA Code:
ws.Cells(lastrow, lastcol).Copy
That worked well! Lol! I was just learning and copying VBA in google and youtube.

Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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