+ Reply to Thread
Results 1 to 8 of 8

Unexpected results in sorting macro

  1. #1
    Registered User
    Join Date
    12-04-2015
    Location
    Chicago, Illnois
    MS-Off Ver
    2013
    Posts
    3

    Unexpected results in sorting macro

    I have a simple macro that compares two columns and puts new entries from the first column (when compared to the second) in to the 3rd column.

    The odd behavior is the last entry in column 3. When I step through the macro, it says Charlie-Brown < Charlie-Brown which it shouldn't be (that's why I sort the columns first). The code I am using appears after the result table below. I've also attached the .xlsm file. I would appreciate any ideas on what is wrong.

    Thanks in advance for any pointers!

    Alpha Alpha Alpha2
    Alpha2 Bravo Bravo2
    Bravo Charlie.Brown Charlie-Brown
    Bravo2 Charlie-Brown
    Charlie.Brown Delta
    Charlie-Brown Echo
    Delta Foxtrot
    Echo Golf
    Foxtrot Hotel
    Golf Indiana

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Unexpected results in sorting macro

    Hi kcorless and welcome to the forum.

    See if the following code does what you are looking for.
    Please Login or Register  to view this content.
    In your code, sorting columns A and B doesn't mean your "less than" will work from row to row.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Re: Unexpected results in sorting macro

    How about this "oldie" writen by Tom Ogli? years and years ago.

    Please Login or Register  to view this content.
    Alf

  4. #4
    Registered User
    Join Date
    12-04-2015
    Location
    Chicago, Illnois
    MS-Off Ver
    2013
    Posts
    3

    Re: Unexpected results in sorting macro

    I know there are several ways to do this. Could you elaborate on your comment that "sorting columns A and B doesn't mean your "less than" will work from row to row." It seems that Charlie.Brown is indeed less that Charlie-Brown if i compare it simply as a formula - i.e. =IF("Charlie.Brown"<"Charlie-Brown","dot","dash") evaluates to yet when the macro runs "Charlie-Brown" < "Charlie-Brown" evaluates to true which throws "Charlie-Brown" out into column 3.

    Thanks
    ken

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Unexpected results in sorting macro

    Step through your code watching your Y and Z variables. It is your assumption that going down your list on the right will always be less than the reduced values on the left. It is like a one off problem.

  6. #6
    Registered User
    Join Date
    12-04-2015
    Location
    Chicago, Illnois
    MS-Off Ver
    2013
    Posts
    3

    Re: Unexpected results in sorting macro

    Quote Originally Posted by MarvinP View Post
    Step through your code watching your Y and Z variables. It is your assumption that going down your list on the right will always be less than the reduced values on the left. It is like a one off problem.
    Hi Marvin - thanks for the help. When i step through the debugger, the problem i get is that IF "Charlie-Brown" < "Charlie.Brown" evaluates to TRUE. But it seems like it should evaluate to FALSE.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Unexpected results in sorting macro

    This worked for me
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Unexpected results in sorting macro

    Another variation
    BTW thx for the file
    Please Login or Register  to view this content.
    - Battle without fear gives no glory - Just try

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Formula produces unexpected results
    By furface00 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-04-2014, 02:17 PM
  2. [SOLVED] Unexpected results from MSQuery
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2014, 11:09 AM
  3. LINEST function unexpected results
    By guffaw320 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-05-2013, 02:46 PM
  4. Unexpected results from CurrentRegion
    By ajgully in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2011, 02:55 PM
  5. VLOOKUP with unexpected N/A results
    By cgjones in forum Excel General
    Replies: 2
    Last Post: 03-15-2011, 04:39 PM
  6. unexpected results when copying a formula
    By plumcloth in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-29-2010, 12:35 PM
  7. Unexpected HLOOKUP results
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2009, 03:01 PM
  8. Unexpected Results with CurrentRegion
    By avveerkar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2007, 09:57 PM

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