# Microsoft Office Application Help - Excel Help forum > Excel Programming / VBA / Macros >  > [SOLVED] Auto Sort Columns on Workbook Open

## stacy

Hey everyone... Since this forum has been so helpful, I thought I would
try another one!

I have a worksheet with 10 columns, and an ever number of growing rows.
What I would like to do is to Sort Column 'B', along with all the
other respective data in the other columns, each time the spreadsheet
opens.  I would prefer to use VBA or some other auto-launching event.

As always, thanks for any help that you guys can provide!!

----------


## Dave Peterson

There are a lot of sort options--I may have not chosen the ones you want.

But this may give you a start:

Option Explicit
Sub auto_open()

Dim myRng As Range

With Worksheets("Sheet1")
Set myRng = .Range("a1:j" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

With myRng
.Cells.Sort key1:=.Columns(2), order1:=xlAscending, _
header:=xlYes
End With

End Sub

I used column A to find the last row of the range to sort.

stacy wrote:
>
> Hey everyone... Since this forum has been so helpful, I thought I would
> try another one!
>
> I have a worksheet with 10 columns, and an ever number of growing rows.
>  What I would like to do is to Sort Column 'B', along with all the
> other respective data in the other columns, each time the spreadsheet
> opens.  I would prefer to use VBA or some other auto-launching event.
>
> As always, thanks for any help that you guys can provide!!

--

Dave Peterson

----------


## stacy

I am sure you are already getting tired of seeing my name in this
forum, huh??  :Smilie: 

I must be doing something incorrectly...  I have the following as a
smaller example of what I will be using.  I copied the code, but it
didn't sort 'C' before 'D' when opened.

School	   School id	Name
A	        1	Angie
B	        2	Barb
D	        4	Maral
C	        3	Brad

The theory is that user Brad has just entered a new school into the
sheet, saved it, and closed it.  I would like "on open", the sheet to
notice that it is out of order, and place the 'C', and all subsequent
information to the right, after the 'B'... Make sense?  Typically users
will go to the end of the sheet, and enter their infomation on a daily
basis, but when I open it to view, I would like it to all be sorted by
the school name... in this case A, B, C, D, etc..

Thanks again for any insight... I am sure I am missing something very
simple.  (still very new to this)...

----------


## Dave Peterson

It worked ok for me with your data.

Did you change the code to point at the correct worksheet?  (I used sheet1.)

Do you have macros enabled?

Did you paste the auto_open sub in a general module of that workbook's project?

If you click somewhere (anywhere) in that subroutine and hit F8 (to step through
the code), what happens?

What is in those cells?  Is it just typed in or is it a formula that directly
points at another cell on a different worksheet, like:
=sheet2!b99

(These kinds of formulas don't work with sorts.)

stacy wrote:
>
> I am sure you are already getting tired of seeing my name in this
> forum, huh??  :Smilie: 
>
> I must be doing something incorrectly...  I have the following as a
> smaller example of what I will be using.  I copied the code, but it
> didn't sort 'C' before 'D' when opened.
>
> School     School id    Name
> A               1       Angie
> B               2       Barb
> D               4       Maral
> C               3       Brad
>
> The theory is that user Brad has just entered a new school into the
> sheet, saved it, and closed it.  I would like "on open", the sheet to
> notice that it is out of order, and place the 'C', and all subsequent
> information to the right, after the 'B'... Make sense?  Typically users
> will go to the end of the sheet, and enter their infomation on a daily
> basis, but when I open it to view, I would like it to all be sorted by
> the school name... in this case A, B, C, D, etc..
>
> Thanks again for any insight... I am sure I am missing something very
> simple.  (still very new to this)...

--

Dave Peterson

----------


## stacy

Okay, i think I located the problem... I had not placed it in a Module,
but instead the Worksheet in the VBA window.  I think it is working
perfectly now... Thanks once again Dave!!!  Huge help!!!

----------


## stacy

Oh wait... One more quick question:  When I sort the spreadsheet
manually, I get the window asking me if I want to sort everything that
looks like a number, as a number, which I do want... Some users have
placed things in here that are not picked up and sorted as a number.
Is there a way to build this into the code?  Thanks again...

----------


## Dave Peterson

Are all your users running xl2002 or above?

If no, then this will break for users of older versions.  (It was added in
xl2002.)

The sort statement has an optional parm that you can add for each key.

DataOption1:=xlSortNormal
or
DataOption1:=xlSortTextAsNumbers

Just add that to the end of the sort statement.

With myRng
.Cells.Sort key1:=.Columns(2), order1:=xlAscending, _
header:=xlYes, DataOption1:=??????????
End With

But the default value is xlSortNormal.  So that range should treat text numbers
and number numbers separately.

I'm not sure what you're asking???


stacy wrote:
>
> Oh wait... One more quick question:  When I sort the spreadsheet
> manually, I get the window asking me if I want to sort everything that
> looks like a number, as a number, which I do want... Some users have
> placed things in here that are not picked up and sorted as a number.
> Is there a way to build this into the code?  Thanks again...

--

Dave Peterson

----------


## stacy

Nope... You hit it right on the mark!!  I have people in this thing
that shouldn't be, and they have entered numbers as text, numbers as
numbers, and numbers as God knows what!!  So when I do a simple "select
column 'B', and sort", it prompts me that some numbers are text, etc,
and how do I want to treat them.  I then tell it to treat anything at
all that resembles a number, as a number, and sort it... Works fine.
Your solution aboves is just what I needed!!!  Works great!!  Needless
to say I am going to have few words with a select group of users making
my world harder than it should be!!  :Smilie: )

Thanks again Dave!!!  Perfect solution!

----------


## Dave Peterson

If it weren't for users, life would be simpler <vbg>.

stacy wrote:
>
> Nope... You hit it right on the mark!!  I have people in this thing
> that shouldn't be, and they have entered numbers as text, numbers as
> numbers, and numbers as God knows what!!  So when I do a simple "select
> column 'B', and sort", it prompts me that some numbers are text, etc,
> and how do I want to treat them.  I then tell it to treat anything at
> all that resembles a number, as a number, and sort it... Works fine.
> Your solution aboves is just what I needed!!!  Works great!!  Needless
> to say I am going to have few words with a select group of users making
> my world harder than it should be!!  :Smilie: )
>
> Thanks again Dave!!!  Perfect solution!

--

Dave Peterson

----------


## merlinium

Hi there, I was wondering if you can help.

Apologies for opening up an old thread but this thread is how I found the site and came up on my google search.

I offer my further apologies as I am not great at using excel but thanks to people like yourselves and sites like this, I am getting better.



Is there any way to apply this formula to all columns?

I have a spreadsheet that will continually be added onto. I would like it to sort automatically upon opening. 

This is good as it sorts one column, is there an option though to sort all columns independently, so that column a is sorted, then column b is sorted, then c is sorted etc.

Thanks for any help I may receive.

----------


## rwmcgill

I have a question about this code.  It is not running on my current machine however I believe thats due to the fact the administration has restrictions.  I can run macros but maybe not upon opening files.

Nonetheless, thats not my issue.  I was reading up on "events" yesterday and decided to pursue the application of it through examples.  I thought this would be an event?  Is it not?  If so, where in this statement is the event of opening the file identified (or declared)?

Thank you


Also, for the sake of understanding the code if anyone wants to explain the items below I would be intersted in a more thorough understanding.  Grazie!






```
Please Login or Register  to view this content.
```

----------


## arlu1201

rwmcgill,

Welcome to the forum.

I have added *code tags* to your post.  As per forum rule 3, you need to use them whenever you put any code in your post.  Please add them in future.  If you need more information on how to use them, check my signature.

Also,

_Your post does not comply with Rule 2 of our Forum_ RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.

----------

