Copy data for a varying number of rows with VBA

Ed_B

New Member
Joined
Mar 28, 2018
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
A worksheet is populated with data in specified columns, but each time the number of populated rows is different. I need a macro to identify and copy the data from the varying ranges that can exist.

Thank you for any suggestions you may have.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Can you show us an example of what your data looks like, and let us know whether or not you want to copy ALL of the data, or just some sub-section of it?
 
Upvote 0
The data is produced in Rows 6 to Unknown # of Rows and in Columns B - O. The data to be copied is in Columns H and I. Another copy is from Columns M, N, O.
Data to be Copied.png
 
Upvote 0
One thing you can do is dynamically find the last row in a column with data, and use that in your copy command.
For example, if you want to the data from columns H:I starting in row 6 down to the last row with data in column H, you could do this:
VBA Code:
Dim lr as Long
lr = Cells(Rows.Count, "H").End(xlUp).Row
Range("H6:I" & lr).Copy
...
 
Upvote 0
Solution
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,215,886
Messages
6,127,585
Members
449,385
Latest member
KMGLarson

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