Hi All,
Im having a little trouble with getting this code to work correctly and was wondering if someone could point me in the right direction.
Here is the code that I am using:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
With Range("A1:I" & Cells(Rows.Count, 2).End(xlUp).Row)
.Sort Key1:=.Cells(1, 2), Order1:=xlAscending, _
Key2:=.Cells(1, 7), Order2:=xlAscending, Header:=xlYes
Cells(.Rows.Count + 1, 1).Select
End With
End Sub
Which works great as is. Basically it sorts my data, then selects the first empty cell when the user saves the workbook.
However, now I have more than one sheet in the book, so if the user is on another sheet then saves the book he starts getting errors.
So I wanted to get the sort to only execute if Sheet1 is active, and heres what I added:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveSheet.Name = "Sheet1" Then
With Range("A1:I" & Cells(Rows.Count, 2).End(xlUp).Row)
.Sort Key1:=.Cells(1, 2), Order1:=xlAscending, _
Key2:=.Cells(1, 7), Order2:=xlAscending, Header:=xlYes
Cells(.Rows.Count + 1, 1).Select
End With
End If
End Sub
Which will save the book but doesnt perform the sort or cell selection after.
Any help with this would be greatly appreciated, thanks!
Bookmarks