Alphabetically Sorting 2x Sheets Using the same Colmn for reference [VBA]

Joined
Jun 13, 2017
Messages
108
Hello guys, I'm having trouble repurposing this macro to include a new sheet ("STAFF INFO") so it sorts that sheet in the exact same way as it would the one in the macro so I don't get mismatched staff names and info.

Mind you in Rota (ORIG) I have the names of everyone working in column A whereas in STAFF INFO all of column A is pretty much a formula that references the cells in ROTA ORIG (e.g. "='ROTA (ORIG)'!A5)

Sub StaffSort()
ActiveSheet.Unprotect Password:="asd"
Range("A5:" & "BF" & Range("C1")).Select
ActiveWorkbook.Worksheets("ROTA (ORIG)").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ROTA (ORIG)").Sort.SortFields.Add Key:=Range( _
"A5:" & "A" & Range("C1").Value), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("ROTA (ORIG)").Sort
.SetRange Range("A5:" & "BF" & Range("C1"))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("C5").Select
ActiveSheet.Protect Password:="asd"
End Sub​
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:
Code:
Sub StaffSort()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets(Array("ROTA (ORIG)", "STAFF INFO"))
        ws.Unprotect Password:="asd"
        ws.Range("A5:" & "BF" & ws.Range("C1")).Select
        ws.Sort.SortFields.Clear
        ws.Sort.SortFields.Add Key:=ws.Range("A5:" & "A" & ws.Range("C1").Value), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ws.Sort
            .SetRange ws.Range("A5:" & "BF" & ws.Range("C1"))
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ws.Protect Password:="asd"
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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