+ Reply to Thread
Results 1 to 9 of 9

Dynamic Range Trouble...rows arent updating??

  1. #1
    Registered User
    Join Date
    06-02-2011
    Location
    Georgia, US
    MS-Off Ver
    Excel 2007
    Posts
    1

    Question Dynamic Range Trouble...rows arent updating??

    I am trying to define a dynamic range after an auto filter is applied, however in the 13th line of the following code, I know I need to replace "R20C10" with something that dynamically reflects the changing Row count. Any help would be appreciated!


    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Dynamic Range Trouble...rows arent updating??

    Try this:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Dynamic Range Trouble...rows arent updating??

    Sorry Mojo, two things...

    1) Never use .UsedRange to determine rows, it "seems" to work most of the time, but it's not working for the reasons you think, so when it fails you won't know it has, and you're data is now corrupted. Best to avoid using .UsedRange method for determining last row of data.

    2) We need to show OPs how to remove the "selecting" and "activating" from their recorded macros 99% of the time.


    ===============

    My solution would use a variable to spot and store the row number of the last row with data looking UP column A:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Dynamic Range Trouble...rows arent updating??

    If you want to see the .UsedRange.Rows.Count method fail, try this:

    1) Open a new empty sheet
    2) Place a value in C5 only
    3) Open the VBEditor
    4) Open the Immediate window (Ctrl-G) and type in this line of code to get the window to show you the answer to your line of code:
    Please Login or Register  to view this content.
    Notice it gives back the answer 1 instead of 5? UsedRange.Rows.Count tells you how many rows are used, NOT what the last row used is. Any blank rows at the top of a sheet will completely destroys this method.

    You could get all fancy and spot the first row used and do some math to make it work, but it's confusing to look at and more calcs than needed:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 06-08-2011 at 07:33 PM.

  5. #5
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Dynamic Range Trouble...rows arent updating??

    Hi Jerry

    Both good points.

    I normally would use .End to find the last row in my own projects. But in answering this question I used .UsedRange because I couldn't guarantee that the last row would always be in column A.

    Am I also right in thinking that .UsedRange will count formatted cells (even if the cell is empty)?

    Dion

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Dynamic Range Trouble...rows arent updating??

    Please Login or Register  to view this content.



  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Dynamic Range Trouble...rows arent updating??

    Quote Originally Posted by mojo249 View Post
    I normally would use .End to find the last row in my own projects. But in answering this question I used .UsedRange because I couldn't guarantee that the last row would always be in column A.

    True, but from a teaching standpoint it's much simpler to novice vba writers to grasp editing the correct column to "look up". It's an easy lesson.

    Am I also right in thinking that .UsedRange will count formatted cells (even if the cell is empty)?
    You are correct.



    SNB shows another good method, but be wary the .CurrentRegion method, too. That will fail if your data has any fully blank rows in the data. If not, and all rows have at least one cell with data, then that would be a great option.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Dynamic Range Trouble...rows arent updating??

    @JB

    100% agreed.

    Just as a question could we state that we can use usedrange 'safely' if

    Please Login or Register  to view this content.
    we can use currentregion 'safely' if

    Please Login or Register  to view this content.

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Dynamic Range Trouble...rows arent updating??

    SNB, absolutely.

    I'm actually a fan of the one-liner solutions you usually strive for, but in truth, I find them impossible to trust since I've taken to "defensive coding" in a lot my macros, the macros solve a standard problem of some sort, but usually require a few extra lines of code to resolve common problems (or test for them) along the way. You know how it goes...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1