Macro to sort

bucci35

Active Member
Joined
Jul 6, 2002
Messages
350
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a workbook with 13 sheets, all of which have 9 "tables" that contain 3 columns each. The data will change over time. I want to be able to create 2 macro's on each sheet that sorts either column b or c. I have no problem creating for sheet 1, however is their a faster way to create the same for the other 12 sheets? Instead of recording the same thing 2 times for all sheets?
Hope this makes sense

Thanks
Dan
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Loop over the sheets you want to apply the sort to. You might be able to simplify with a loop that uses a counter and the sheet code name. If you see Sheet6(My Data) in the vb editor, Sheet1 is the code name, My Data is what you see on the sheet tab. However, I've never figured out how to concatenate a counter variable to the code name and get it to work. I have seen a solution where all the sheets to be looped had 2 digits in the code name (so Sheet01, not Sheet1). Then it seems possible to look at the last 2 characters of the name in a loop.
 
Upvote 0
See if this correctly picks the 13 sheets (assuming you can use the sheet codename). Change 2 in the counter accordingly. If it does, then you can incorporate the sheet1 code you already have.
VBA Code:
Sub loopSheets()
Dim ws As Worksheet
Dim i As Integer
Dim strName As String

On Error GoTo errHandler
i = 1

For i = 2 To 14
     strName = "Sheet" & i
     Set ws = Worksheets(ActiveWorkbook.VBProject.VBComponents("Sheet" & i).Properties("Index"))
     Debug.Print ws.CodeName & "  " & ws.Name
Next
exitHere:
Set ws = Nothing
Exit Sub

errHandler:
If Err.Number = 9 Then
     Resume Next
Else
     MsgBox "Error " & Err.Number & ": " & Err.Description
     Resume exitHere
End If
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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