Results 1 to 5 of 5

Thread: Sort help

  1. #1
    Conjurer Simi's Avatar
    Join Date
    Feb 2012
    Location
    Utah, USA
    Posts
    190
    Articles
    0
    Excel Version
    Version 2002 Build 12527.20194

    Sort help



    Register for a FREE account, and/
    or Log in to avoid these ads!

    I have the following code that sorts properly the first time I run through. However if I run the code again it errors out.
    The first 2 rows of my sheet contain header information and should not be included in the sort.

    Code:
    With Worksheets("W-I-P")
            iTotalRowSource = .Cells(.Rows.Count, "D").End(xlUp).Row
            iTotalColSource = .Cells(1, .Columns.Count).End(xlToLeft).Column
                
            .Range(Cells(3, 1), Cells(iTotalRowSource, iTotalColSource)).Sort _
                Key1:=.Range("AL3"), Order1:=xlDescending, _
                Key2:=.Range("AG3")
        End With

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,810
    Articles
    0
    Excel Version
    365
    I can't reproduce this error, can you supply a file where this occurs?

  3. #3
    Conjurer Simi's Avatar
    Join Date
    Feb 2012
    Location
    Utah, USA
    Posts
    190
    Articles
    0
    Excel Version
    Version 2002 Build 12527.20194
    While cleaning up my sensitive data to post a sheet I had an idea.

    It seems that this sort worked correctly after I imported the W-I-P sheet, thus making it the active sheet.
    When running the code a 2nd time, it skips the import and just tries to sort. However the active sheet is not the W-I-P.

    I fixed this by simply putting Worksheets("W-I-P").Activate before the with block.

    If there is a better way to do it I would still be interested in knowing how to sort a sheet that is not the active sheet.

    Thank you for looking at this p45cal.

  4. #4
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,810
    Articles
    0
    Excel Version
    365
    change:
    .Range(Cells(3, 1), Cells(iTotalRowSource, iTotalColSource)).Sort Key1:=.Range("AL3"), Order1:=xlDescending, Key2:=.Range("AG3")
    to:
    .Range(.Cells(3, 1), .Cells(iTotalRowSource, iTotalColSource)).Sort Key1:=.Range("AL3"), Order1:=xlDescending, Key2:=.Range("AG3")

  5. #5
    Conjurer Simi's Avatar
    Join Date
    Feb 2012
    Location
    Utah, USA
    Posts
    190
    Articles
    0
    Excel Version
    Version 2002 Build 12527.20194
    Ahhh yes missing "." dang I seem to do that a lot.
    Thank you p45cal

Posting Permissions

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