Results 1 to 9 of 9

Thread: [RESOLVED] VB 6.5 Data sort function

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    7

    Resolved [RESOLVED] VB 6.5 Data sort function

    Hi,

    I'm new to VB and I'm trying to sort a region of data in Excel using the following code

    Sheets(1).Range("A6").Select
    ActiveCell.CurrentRegion.Select
    Selection.Sort Key1:=Range("H2"), Order1:=xlAscending, _
    Key2:=Range("D2") , Order2:=xlAscending, Key3:=Range("B2"), _
    Order3:=xlAscending, Header:= xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,_
    DataOption2:=xlSortNormal, DataOption3:= xlSortNormal

    but I keep getting an error message which says 'Identifier under cursor not recognised' and highlights the code in yellow, or which says 'The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank'. I am not that new to sorting data but I've been completely unable to solve this - if anyone has any idea about what this could be, I would greatly appreciate it.

    Thanks,

    Kerry

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: VB 6.5 Data sort function

    What range is your data in?

    When I try this, it works fine (my data is in a1:k6).

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB 6.5 Data sort function

    VB 6.5 is not VB.NET. I think it's VBA but I'm not 100% sure. The fact that you're working with Excel supports that theory. In that case, this thread should be in the VBA forum, where I have asked the mods to move this thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB 6.5 Data sort function

    Welcome to VBForums

    Thread moved to the 'Office Development/VBA' forum... note that while it certainly isn't made clear, the "VB Editor" in Office programs is actually VBA rather than VB
    Quote Originally Posted by jmcilhinney View Post
    VB 6.5 is not VB.NET. I think it's VBA but I'm not 100% sure.
    You are correct... only VB 6.0 is VB6, all other 6.x are VBA
    I have asked the mods to move this thread.
    Thanks as always.
    Last edited by si_the_geek; Nov 22nd, 2011 at 03:12 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    7

    Re: VB 6.5 Data sort function

    My range is A6 to U30..I can see no reason why my code doesn't work! Perhaps there is a problem with the file. Thanks for your reply..

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB 6.5 Data sort function

    I think I see the problem... on the .Sort line you are using the same unsafe coding style that Excel records macros in (eg: Range("H2") , etc), rather than the proper syntax as you've got on the first line of your code (which would make it: Sheets(1).Range("H2") , etc).

  7. #7
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: VB 6.5 Data sort function

    When I copy your code verbatim into a macro, there is one thing that immediately causes an error. In your second to last line you are missing a space in front of the underscore at the end:

    Code:
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,_
    When I add the space there, the macro works fine for me.

    What version of Excel are you using?

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    7

    Re: VB 6.5 Data sort function

    Hi,

    Si_the_Geek thanks you have solved my problem - vbfbryce it is strange that some people seem to be able to use 'unsafe' code to sort data so that yours works and mine doesn't after you corrected my second error! But anyway it is resolved so thank you very much for your help.

    Kerry

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB 6.5 Data sort function

    I'm happy to help
    Quote Originally Posted by KJDB1276 View Post
    it is strange that some people seem to be able to use 'unsafe' code to sort data so that yours works and mine doesn't
    That is due to a bit of luck.

    If you don't specify a parent object (in this case the Sheet), Excel will make a guess as to what you meant - and if you have a different file active (or are on the right file, but the wrong sheet) then it is highly likely to make the wrong guess, and therefore fail in some way (often an error, but sometimes by silently editing the wrong file).

    In this particular case the error is because it ends up trying to sort the values based on columns that aren't even on the same sheet.
    But anyway it is resolved so thank you very much for your help.
    In that case could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

Posting Permissions

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



Click Here to Expand Forum to Full Width