+ Reply to Thread
Results 1 to 4 of 4

VBA Code for Sort - Help!

  1. #1
    Registered User
    Join Date
    12-03-2008
    Location
    Mumbai
    Posts
    1

    VBA Code for Sort - Help!

    Hi - I'm a VBA programming newbie and could do with some help. I'm sorry if this is a dumb question but I could not google out a solution.

    I have written a macro that requires data to be sorted on the basis of two keys. Since I did not know how to start from scratch, I recorded a sort and tried to re-use the code. The modified code is posted below. Before that,

    sheetName = worksheet name (e.g. "Sales Data")
    colNo1 = 1st sort key (e.g. "C")
    colNo2 = 2nd sort key (e.g. "I")
    flag = just something I use to prevent the last row from being sorted in some cases

    The function lastRowOfWorksheet returns, well, the last row.

    Here goes -

    Please Login or Register  to view this content.
    Now what's happening is that this VBA sort sometimes works fine and the macro produces the right results. Sometimes the macro doesn't do what it's supposed to. That's because the VBA sort fails to sort the data. When I manually sort the data and run the macro, it produces expected results. I'm not able to understand why the VBA sort is not working at times. The range and the last row function are correct.

    Can somebody please point out what the problem could be? My OS is Windows XP and I'm using Office 2007.

    Thanks a lot for your time.
    Last edited by R^3; 12-03-2008 at 05:00 AM. Reason: Adding code tags

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    Please use code tags - the # sign when you writing a post - as it makes code much easier to read, not to mention it being one of the rules here.

  3. #3
    Registered User
    Join Date
    11-18-2008
    Location
    Texas
    Posts
    2
    I'm having the same problem that R^3 is experiencing. Has anyone found a solution?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Take a few minutes to read the forum rules, and then start your own thread, please. You can add a link to this one if you think it's relevant.
    Entia non sunt multiplicanda sine necessitate

+ 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