Sorting multiple sheets with variable rows

peee2

New Member
Joined
Jun 4, 2019
Messages
14
Hi All,
The following code has some issues.it is working when all rows are same but whenever it needs to sort a sheet with variable rows , it is selecting the cells but not sorting. Pls Help.

Code:
Sub Sort()




    Dim i As Long
    Dim LastARow As Integer
    LastARow = Range("A11").End(xlDown).Offset(-3, 0).Row
    
    
    For i = 14 To Worksheets.count
    Rows("11:11").Select
    Range(Selection, Selection.End(xlDown).Offset(-3, 0)).Select


        With Worksheets(i).Select
        With Worksheets(i).Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C11:C" & LastARow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
        .SortFields.Add Key:=Range("A11:A" & LastARow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
        .SortFields.Add Key:=Range("B11:B" & LastARow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
 
        .SetRange Range("A11:IU" & LastARow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    
       End With
       End With
       
    Next
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This seems to be a code sequencing problem. You might have assumed that the code will always start from a particular point (and at the With Worksheets(i).Select statement that point is set), but prior to that point, at the instant that your Range Selection is made ... where is the code operating from? It's from some place that may only coincidentally be correct ... and after the code starts running, it's not correct, because until you do your Worksheets(i).Select, you're operating at Worksheets(i‑1).

If you start the code running at Worksheets(i), then it seems to work properly for that worksheet, but if you start at some other worksheet with a row count different from Worksheet(i), then Worksheet(i) will also be performed incorrectly.

Select your worksheet before you count your End(xlDown).Rows count.
 
Last edited:
Upvote 0
Ok..will select the sheet from where i want to start the sorting and try running it..but den doesnt i=14 means it will start from 14th sheet in the workbook
 
Last edited:
Upvote 0
As you've got it written, yes, code operation will start from sheets(14). But why did you write it so if you didn't want it to start from there? It's also possible to keep the code as it is and to start exactly how you are, BUT without telling it explicitly where to start, you're at the mercy of wherever the focus happens to be when the code does start to operate. (Which may not always be up to you, if the file will be used by others or if some other procedure calls the code from another location.)

In any case, when you've selected a new Worksheet to begin operation, that's when you'll want to count the rows / range over which to operate, not before you get there and examine it.
 
Upvote 0
How about
Code:
Sub Sort()
   Dim i As Long
   Dim LastARow As Integer
   
   
   For i = 14 To Worksheets.Count
      With Worksheets(i)
         LastARow = .Range("A11").End(xlDown).Offset(-3, 0).Row
         .Rows("11:11").Select
         .Range(Selection, Selection.End(xlDown).Offset(-3, 0)).Select
         With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("C11:C" & LastARow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                  xlSortNormal
            .SortFields.Add Key:=Range("A11:A" & LastARow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
                  xlSortNormal
            .SortFields.Add Key:=Range("B11:B" & LastARow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                  xlSortNormal
   
            .SetRange Range("A11:IU" & LastARow)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
         End With
      End With
   Next i
End Sub
 
Upvote 0
Sir i wanted to start sorting from my 14th sheet only....anyways will try n make the changes as per your advice..thanks
 
Upvote 0
no..its not working. gives a message saying run-time error 1004: Select method of range class failed
 
Upvote 0
As Bluehornet had suggested..just selected the sheet and now the code seems to be working fine.Thanks Bluehornet and fluff
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top