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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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