VBA Last row copy

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Code:
Range("AG2:BC1000").Copy Sheets("Data").Cells(Sheets("Data").Rows.Count, "D").End(xlUp).Offset(1, 0)

The code above copy and pastes the range specified from the current sheet to the data sheet.

To improve this I would like it to copy the range down to the last populated row but this could be in either column AG, AK, AO, AS, AW or BA.

Any idea how I could achieve this please?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
From Abdo's Post at Stack Overflow

The best way to get number of last row used is:
Code:
<code style="margin: 0px; padding: 1px 5px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: pre-wrap;">ActiveSheet.UsedRange.Rows.Count</code>
for the current sheet.

Code:
[COLOR=#242729][FONT=Arial]<code style="margin: 0px; padding: 1px 5px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Worksheets("Sheet name").UsedRange.Rows.Count</code>
for a specific sheet.[/FONT][/COLOR]
To get number of the last column used:
Code:
[COLOR=#242729][FONT=Arial]<code style="margin: 0px; padding: 1px 5px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: pre-wrap;">ActiveSheet.UsedRange.Columns.Count</code>
for the current sheet.[/FONT][/COLOR]
Code:
[COLOR=#242729][FONT=Arial]<code style="margin: 0px; padding: 1px 5px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Worksheets("Sheet name").UsedRange.Columns.Count</code>
for a specific sheet.[/FONT][/COLOR]
I hope this helps.
 
Upvote 0
@jamescooper, is there data in the other columns that could use more rows than the ones that you have specified?
 
Upvote 0
Are the columns that could be longer within the column range AG:BC or outside the range?
 
Upvote 0
Assuming that the longer columns are outside the columns stated range then see if the below gives the correct last row...
Code:
Debug.Print Columns("AG:BC").Find("*", , xlValues, , xlByRows, xlPrevious).Row
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

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