+ Reply to Thread
Results 1 to 5 of 5

Problem with sort through VBA

  1. #1
    Registered User
    Join Date
    11-19-2012
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    9

    Problem with sort through VBA

    I am trying to sort the values in rows 3 through the end of the column in a worksheet (named "parms") using VBA:

    Please Login or Register  to view this content.
    It seems to work fine when lastline > 3 (i.e., it properly sorts I3 through the end of the column), but when lastline = 3, it goes rogue and not only sorts I1:I3 instead of I3:I3 but also sorts H1:H3 and G1:G3. What the heck is going on?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: when "sort" goes rogue...

    You can test lastline before sorting.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-19-2012
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Problem with sort through VBA

    Thanks, shg. I surely can test before sorting. But I'm new to this whole VBA/Excel thing, and it makes me nervous that it would display such seemingly random and irrational behavior. Is it thinking something along the lines of: "He can't really want me to sort I3:I3. What kind of fool would want to sort one value? He must want to sort I1:I3, H1:H3, and G1:G3 instead."?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Problem with sort through VBA

    I think that behavior has changed since Excel 2003; it used to generate a run-time error if you tried to sort one cell (or one row if sorting top to bottom).

    Now the behavior is akin the the way Excel behaves under other circumstances when only a single cell is selected, and the sort is applied to the current region of the cell. Makes sense to me.

  5. #5
    Registered User
    Join Date
    11-19-2012
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Problem with sort through VBA

    Interesting! I would never have guessed exactly what Excel would do...only that it would be different from what I'd expect (actually, I expect it to do what I expect it to. But when it doesn't, it isn't a big surprise.)
    Last edited by Bob Berg; 11-21-2012 at 01:47 PM.

+ 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