Consulting

Results 1 to 10 of 10

Thread: Solved: Sort without selecting the Sheet

  1. #1
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location

    Solved: Sort without selecting the Sheet

    Can this code be modified to run (run meaning be called by another macro) so that the sheet does not have to be selected?

    Thanks...

    Jim

    [vba]
    Sub SortSheets()
    Range("A6").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Sort Key1:=Range("A6"), Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    Range("A4").Select
    End Sub
    [/vba]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    This should give the same result:[vba]Sub SortSheets(Sht As Worksheet)
    Set myRng = Sht.Range("A6")
    Range(Range(myRng, myRng.End(xlToRight)), Range(myRng, _
    myRng.End(xlToRight)).End(xlDown)).Sort Key1:=myRng, _
    Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
    MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    End Sub[/vba]call with the likes of:[vba]Sub test()
    SortSheets Sheets("Sheet1")
    End Sub[/vba]but note that both your, and this, code sorts a block of cells the size of which is dependent on the lack of empty cells in the top row and the lack of spaces in the first column below A6.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location
    What if I wanted this to run on any sheet that's name begins with the name "Sheet"?

  4. #4
    If Left(Sht.Name, 5) = "Sheet" Then
    'Your code goes here
    End if
    Post Questions - Get Answers

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by JimS
    What if I wanted this to run on any sheet that's name begins with the name "Sheet"?
    "Any" you need to specify the sheet. "All" you can loop using the suggested code.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by mumin_abdul
    If Left(Sht.Name, 5) = "Sheet" Then
    'Your code goes here
    End if
    which becomes[vba]Sub test()
    For Each sht In ActiveWorkbook.Sheets
    If Left(sht.Name, 5) = "Sheet" Then
    SortSheets sht
    End If
    Next sht
    End Sub
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    The code would most likely still fail, as the sort procedure doesn't reference the worksheet on all of the range references. Change it to...

    [vba]Sub SortSheets(Sht As Worksheet)
    Set myRng = Sht.Range("A6")
    Sht.Range(Sht.Range(myRng, myRng.End(xlToRight)), Sht.Range(myRng, _
    myRng.End(xlToRight)).End(xlDown)).Sort Key1:=myRng, _
    Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
    MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    End Sub[/vba]

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Until recently I was never sure whether I needed to include the explicit reference to the sheet throughout as in
    Sht.Range(myRng, myRng.End(xlToRight)
    but I'm beginning to think you don't if there is the full ref within parentheses, which there is throughout in this case, so
    Range(myRng, myRng.End(xlToRight)
    seems to be just fine.
    I tried it with the code I posted in this thread on multiple sheets and it seems to work.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yes, that will work. I still don't recommend it, as it's confusing, and as far as I'm concerned sloppy. Just MHO.

  10. #10
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location
    Thanks this works perfect...

Posting Permissions

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