+ Reply to Thread
Results 1 to 4 of 4

Sort Function

Hybrid View

  1. #1
    Registered User
    Join Date
    06-09-2007
    Posts
    29

    Sort Function

    I'm setting up a public function that sorts after i add an item

    Public Function Sort2()
    Dim ws2 = worksheet
    Set ws2 = Sheets("REGION")
            
    ws2.Range("A1:A5000").Select
    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End Function
    This function works on the other sort i've done on the GOV sheet, but now it doenst work here.

    Any ideas why this wouldnt work. It says that it is a select method of range class failed error.

    Thanks

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try changing

    Selection.Sort Key1:=Range("A1")
    to

    Selection.Sort Key1:=ws2.Range("A1")
    HTH

    rylo

  3. #3
    Registered User
    Join Date
    06-09-2007
    Posts
    29
    Thanks for getting back to me.

    Public Function Sort1()
    Set ws1 = Sheets("GOV")
            
    ws1.Range("A6:K5000").Select
    Selection.Sort Key1:=ws1.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End Function
    This one works... however this one still doesnt

    Public Function Sort2()
    Set ws2 = Sheets("REGION")
            
    ws2.Range("A1:A18").Select
    Selection.Sort Key1:=ws2.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End Function
    It is basically the same, however the second one comes up with errors. Any ideas?

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    I'd assumed that you were actually on ws2 when running the macro. If you are not then try

    Set ws2 = Sheets("REGION")
            
    ws2.Range("A1:A18").Sort Key1:=ws2.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    rylo

+ 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