How can I build a macro to auto detect number of rows?

Spawn10

New Member
Joined
Nov 29, 2011
Messages
7
I am new to excel programming and macros...I am trying to create a macro that will extract data in certain fields and cells, into another sheet. Right now, my approach has been using the recorder functionality to record the steps required to carry out the task. However, I have run into a snag...I have data for different months in the year and each one of them has a different number of rows, to correspond with the number of days in each month. Right now, when I do the recorder for a month that has 31 days, I record 31 rows, but when i try to run the macro for a month that is less than 31 days, i get extra data from the last 31 one day month...filling in the extra days up to 31. As an example, when I run the macro for march data, I get the complete 31 rows. When I then run it for Aprils' data, I still get the extra 31st row with the data from march on there. Rows 1 through 30 will have Aprils' data, but row 31 still shows up...with the data for march.
I guess my question is this...is there a way to program this to auto detect how many rows are available on the sheet that holds the raw data? That way, it can always grab the right number of rows for each month...and only those days for that particular month?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
so you just need to get the last row of a specified column?

you can use the syntax like:

Code:
lastRow=cells(rows.count,1).End(xlUp).Row

that would find the last row in column A assuming that the very last cell in the range is blank. this is something that bothers me and so a more rigorous approach is something like

Code:
if cells(rows.count,1).formula=vbnullstring then
    lastRow=cells(rows.count,1).End(xlUp).Row
else
    lastRow=rows.count
endif
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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