Sort excel when workbook opened

L

LTOSH

I have a workbook with currently 11 worksheets. I am wanting to
automatically sort Column A of each worksheet. I have placed the following
code within my workbook:

Private Sub Workbook_Open()
Worksheets("Abs").Select
Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, header:=xlyes
End Sub

:and it works fine but ONLY with the first worksheet becuase it is named ABS.
What do i need to put in the code to also sort the other worksheets??
(Sample names of other worksheets...Back, Biceps, Cardio)

thanks!
LTOSH
 
J

Jacob Skaria

Try the below instead..

Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Me.Worksheets
ws.Cells.Sort Key1:=ws.Range("A1"), _
Order1:=xlAscending, Header:=xlYes
Next
End Sub

If this post helps click Yes
 
L

LTOSH

worked perfectly thank you so much!!!!!!

Jacob Skaria said:
Try the below instead..

Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Me.Worksheets
ws.Cells.Sort Key1:=ws.Range("A1"), _
Order1:=xlAscending, Header:=xlYes
Next
End Sub

If this post helps click Yes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top