Forum Discussion
Arcade_Printing
Dec 20, 2022Copper Contributor
VBA code is sorting on some worksheet but not all
i have a vba code to search out a worksheet and then sort by date. it was working on the original 15 sheets i had in the workbook. when i copied and added a 16th worksheet it does not run the sort on...
Arcade_Printing
Dec 20, 2022Copper Contributor
flexyourdata
Dec 20, 2022Iron Contributor
Maybe consider replacing your existing code with a loop through all the sheets, rather than repeating the code for each sheet. This will make it easier to keep consistent and debug.
You could try this (not tested):
Public Function SortSheetsInGroup30()
Dim wb As Workbook
Dim ws As Worksheet
Dim sheetsToSort() As String
Dim addressToSort As String
addressToSort = "F10:L608"
'put any sheet names you want to sort comma-separated in the first parameter of Split
sheetsToSort = Split("14,18,11,20,20.5,23,24,25,26,27,28.5,29.25,30,33,0,22,12.5", ",")
Set wb = Workbooks("30#group.xlsm")
For Each ws In wb.Worksheets
If Not IsError(Application.WorksheetFunction.Match(ws.Name, sheetsToSort, 0)) Then
'the sheet is in the sheetsToSort array
'sort it
With ws.Sort
.SetRange ws.Range(addressToSort)
.Header = x1Yes
.MatchCase = False
.Orientation = x1TopToBottom
.SortMethod = x1PinYin
.Apply
End With
End If
Next ws
End Function
Call that function from your OnChange event in the control workbook.