Date order on multiple sheets

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi Please help, I have the code below where I have multiple sheets rows from A:Q and the source is B which is the date what I want from oldest to latest, B1 is the header so it should start from B2.

I am gettng an error on the 'Next'

Code:
Private Sub CommandButton2_Click()
Dim wksht As Worksheet
    For Each wksht In ThisWorkbook.Worksheets
        wksht.Select

With Range("A2:Q" & Cells(Rows.Count, "A").End(xlUp).Row)
.Sort Key1:=Range("B2"), _
Order1:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers
End With
  Next
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hello Patriot,

Try fully qualifying the code as follows:-


Code:
Private Sub CommandButton2_Click()

        Dim wksht As Worksheet
        
For Each wksht In ThisWorkbook.Worksheets
        

With wksht.Range("A2:Q" & wksht.Cells(Rows.Count, "A").End(xlUp).Row)
        .Sort Key1:=wksht.Range("B2"), _
        Order1:=xlAscending, _
        Header:=xlNo, _
        OrderCustom:=1, _
        MatchCase:=False, _
        Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
End With

Next wksht
  
End Sub

........or you could try this abbreviated version:-


Code:
Private Sub CommandButton2_Click()

    Dim ws As Worksheet

For Each ws In Worksheets
    ws.Range("A2", ws.Range("Q" & ws.Rows.Count).End(xlUp)).Sort ws.[B2], 1
Next ws

End Sub


I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
You're welcome Patriot. I'm glad to have been able to assist.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

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