I have the following code which on workbook opening sets a password, sorts the data in a worksheet and hides some lines. I want to apply this to two other worksheets in the same workbook call YEAR ONE & FOUNDATION but cant seem to get it to work. Anybody got any advice or pointers. After the sorting I still need it to select cells A1:A5 in YEAR TWO worksheet.
Private Sub Workbook_Open() Dim ws As Worksheet For Each ws In Worksheets ws.Protect "mark", UserInterfaceOnly:=True Sheets("YEAR TWO").Select ActiveWindow.DisplayGridlines = False Rows("145:900").Select Selection.EntireRow.Hidden = True ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollRow = 6 Range("A1:A5").Select Range("A6:GD123").Select Selection.Sort Key1:=Range("C6"), Order1:=xlDescending, Key2:=Range("D6") _ , Order2:=xlAscending, Key3:=Range("A6"), Order3:=xlAscending, Header:= _ xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _ xlSortNormal ActiveWindow.ScrollRow = 6 Range("A1:A5").Select Next ws End Sub
I have tried the following hoping it would sort YEAR ONE then move to YEAR TWO worksheet and sort that but I get an error, I'm just starting out in VBA and don't really understand what is wrong.
Any help gratefully recieved.
Private Sub Workbook_Open() Dim ws As Worksheet For Each ws In Worksheets ws.Protect "mark", UserInterfaceOnly:=True Sheets("YEAR ONE").Select ActiveWindow.DisplayGridlines = False Rows("145:900").Select Selection.EntireRow.Hidden = True ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollRow = 6 Range("A1:A5").Select Range("A6:GD123").Select Selection.Sort Key1:=Range("C6"), Order1:=xlDescending, Key2:=Range("D6") _ , Order2:=xlAscending, Key3:=Range("A6"), Order3:=xlAscending, Header:= _ xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _ xlSortNormal ActiveWindow.ScrollRow = 6 Range("A1:A5").Select Sheets("YEAR TWO").Select ActiveWindow.DisplayGridlines = False Rows("145:900").Select Selection.EntireRow.Hidden = True ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollRow = 6 Range("A1:A5").Select Range("A6:GD123").Select Selection.Sort Key1:=Range("C6"), Order1:=xlDescending, Key2:=Range("D6") _ , Order2:=xlAscending, Key3:=Range("A6"), Order3:=xlAscending, Header:= _ xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _ xlSortNormal ActiveWindow.ScrollRow = 6 Range("A1:A5").Select Next ws End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks