Anyone know VBA for MS Excel?

If so, here is a little project for work I could use some help on…

It is fairly common on the websites to have the ability to click on a column heading in a table and have the data be sorted by said column.

If you click a second time, it just sorts it the other way.

Is there somehow I can do this in MS Excel?

It would likely include writing some VBA, but my only VBA experience is with MS Access?!

Can someone help me to do this?

Thanks,

TomTees

Of course there is a way to do it! You can use the VBA sort property. It is like hitting the A-Z or Z-A sorting button :slight_smile:

If you want to see the VBA code, you can record a Macro, then hit Alt+F11 (to go to the development window) and see what code has been inserted. It is always good to impreve its VBA because when you record macros, it insert a lot of rubbish that you can do without :wink:

I must say, I’m the tiniest bit unclear on what you’re after.

In excel 2007, if you format a range as a table and include a header in the process, it associates sorts and filters with each column in the table (via a dropdown on the header)

If you want two custom sorts selected by a conditional and triggered by a simple mouseclick, I’m afraid I’ve got no insight. Scallio’s suggestion of customizing a recorded macro is the way I’d go

That option is also available in previous versions of Excel (menu Data > Filters > Autofiler… well, at least in the Spanish version is called “autofiltro” :smiley: This is a direct translation)

In this case, you’re taking about automatic filters which can both filter and sort. That could be another approach by using a different object.

I don’t know if you can automate it by clicking on the headers, but you can sort data by selecting it all and then in the data tab select “sort”.

If you want to automate this I’d suggest recording a macro while you do what I said above so you get a general outline of VBA code you can customize. In my experience that’s way simpler and faster than having to find out everything for yourself :slight_smile: