G
Guest
Someone was great enough to help me out yesterday with some code, but it's
not working 100%. Could someone please help me out? I have a multi-user,
multi-sheet spreadsheet that I am trying to have a macro sort the data in
each sheet when the user goes to save the file. The code I have is stated
below.
1) It currently sorts all data, including the header row, which I don't want
to sort in.
2) it errors out on the first line containing the ".Sort key1...". the error
is "Run-time error 1004: Application-defined or object-defined error"
Any ideas or solutions will make my life much better!!!! Thank you sooo much
and may you have a blessed Christmas Day!
Here's the code!
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
'
Dim WS As Worksheet
Set WS = Worksheets("Kathryn")
With WS.UsedRange
..Sort key1:=.Range("Project"), order1:=xlAscending
..Sort key2:=.Range("Assignment"), order2:=xlAscending
End With
Set WS = Worksheets("Dave")
With WS.UsedRange
..Sort key1:=.Range("Project"), order1:=xlAscending
..Sort key2:=.Range("Assignment"), order2:=xlAscending
End With
Set WS = Worksheets("Ann")
With WS.UsedRange
..Sort key1:=.Range("Project"), order1:=xlAscending
..Sort key2:=.Range("Assignment"), order2:=xlAscending
End With
End Sub
not working 100%. Could someone please help me out? I have a multi-user,
multi-sheet spreadsheet that I am trying to have a macro sort the data in
each sheet when the user goes to save the file. The code I have is stated
below.
1) It currently sorts all data, including the header row, which I don't want
to sort in.
2) it errors out on the first line containing the ".Sort key1...". the error
is "Run-time error 1004: Application-defined or object-defined error"
Any ideas or solutions will make my life much better!!!! Thank you sooo much
and may you have a blessed Christmas Day!
Here's the code!
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
'
Dim WS As Worksheet
Set WS = Worksheets("Kathryn")
With WS.UsedRange
..Sort key1:=.Range("Project"), order1:=xlAscending
..Sort key2:=.Range("Assignment"), order2:=xlAscending
End With
Set WS = Worksheets("Dave")
With WS.UsedRange
..Sort key1:=.Range("Project"), order1:=xlAscending
..Sort key2:=.Range("Assignment"), order2:=xlAscending
End With
Set WS = Worksheets("Ann")
With WS.UsedRange
..Sort key1:=.Range("Project"), order1:=xlAscending
..Sort key2:=.Range("Assignment"), order2:=xlAscending
End With
End Sub