Page 1
Page 1
Started By
Message

Excel Macro Help needed... Spreadsheet w row count that changes over time

Posted on 2/18/19 at 8:39 am
Posted by Crow Pie
Neuro ICU - Tulane Med Center
Member since Feb 2010
25317 posts
Posted on 2/18/19 at 8:39 am
I have a payroll spreadsheet that grows over time (after each payroll) I have been manually sorting it to remove old, unneeded and/or duplicate data and recently wrote a basic Macro that will sort and filter what I want. It works great on whatever sheet I write it in and saves alot of time.

I now have run into the problem that when the spreadsheet has more rows that the one I wrote it in... it errors out. When I tried to debug I can see the row parameters are set statically.

Q) How can I tell the Macro to adjust the range based on what row count is there when the spreadsheet is opened?

I have tried using control /arrow down as the first step to get to the last row cell but it does not seem to dynamically adapt to the spreadsheet as it grows
Posted by Brisketeer
Texas
Member since Aug 2013
1436 posts
Posted on 2/18/19 at 9:29 am to
Assuming this is structured in a tabular manner, can you just write a function to find the first blank row in column A?

However, what you're doing sounds best suited for a database solution like Access.
Posted by LSUtigerME
Walker, LA
Member since Oct 2012
3796 posts
Posted on 2/18/19 at 10:51 am to
You can modify the macro to loop down looking for the first blank row, or make a dynamic variable to find the last row using xlUp.
Posted by oklahogjr
Gold Membership
Member since Jan 2010
36761 posts
Posted on 2/18/19 at 11:07 am to
use a row count on a column that always has data to get the total size and go from there
Posted by Crow Pie
Neuro ICU - Tulane Med Center
Member since Feb 2010
25317 posts
Posted on 2/18/19 at 7:09 pm to
quote:

make a dynamic variable to find the last row using xlUp.
"xlUp "....to set the range of cells?
This post was edited on 2/18/19 at 7:12 pm
Posted by LSUtigerME
Walker, LA
Member since Oct 2012
3796 posts
Posted on 2/18/19 at 8:12 pm to
Use that command to find the last row of data. It goes to the last row in Excel, and looks upward for the last used row.

LastRow = Sht.Range(“A” & Rows.Count).End(xlUp).Row
Or
= Sht.Cells(Rows.Count,1).End(xlUp).Row

This will give you the row number of the last entry in Column A. It’s more reliable than looking down if you have blanks in your data, but of course is vulnerable if you have blanks in the last entry of the column you’re searching.

Whatever the static number that was being used for last row can be replaced by this function.
Posted by CubsFanBudMan
Member since Jul 2008
5071 posts
Posted on 2/19/19 at 1:29 pm to
What about formatting your range as a table. Table references are always dynamic.
Posted by lynxcat
Member since Jan 2008
24151 posts
Posted on 2/19/19 at 3:37 pm to
Yep, my first thought was make your data a table so it’s dynamic.
first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram