Apply VBA to Select Worksheets

lcmilkbone

New Member
Joined
Oct 29, 2021
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hi All,
I'm attempting to apply the below code to 26 out of the total 35 sheets. I can't seem to figure it out. Currently the macros is setup to apply to all sheets but I need to either edit it to exclude the eight tabs that it will not apply to, or edit it to apply to the 26 sheets themselves. I do plan on hiding the 26 tabs as they feed into a summary sheet so would this affect the formula?

For context, in each worksheet that requires the sort, columns B to Q have data and I'm sorting by ticker in column C (ascending). Any thoughts here would be greatly appreciated.

Thanks!

VBA Code:
Sub SortAllSheets()

  Dim WS As Worksheet
  ActiveSheet.Range("B1:Q1").Select
  Selection.Copy
  On Error Resume Next
  Application.ScreenUpdating = False
  For Each WS In Worksheets
    WS.Range("B2:Q500").Sort Key1:=WS.Columns("C"), Order1:=xlAscending
  Next WS
 
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I will also mention that each of the 26 worksheets that require sorting all contain the same number of columns (B:Q) and same header row (2). The only difference is the number of total rows - as each worksheet is for an investment account so each worksheet may have 80-150 rows.
 
Upvote 0
Try
Code:
Sub Maybe()
Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        Select Case ws.Name
            Case "Sheet1", "Sheet2", "Sheet5", "Sheet8"    '<---- Excluded sheet names. Extend as needed
                Case Else
            ws.Range("B2:Q" & ws.UsedRange.Rows.Count).Sort Key1:=ws.Columns("C"), Order1:=xlAscending
         End Select
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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