Hi,
I was wondering if there is a way to auto sort columns in a document.
Thanks in advance!
Turn on the macro recorder, let it record you highlighting all the data to sort, then turn off the recorder.
Now open the VBEditor (Atl-F11) and open the MODULE that was created (left pane) that now has your macro in it.
Copy all the code between the Sub / End Sub.
Doubleclick on the SHEET module (left pane) where you want this to work all the time and the sheet module will open.
At the top of the module enter the following header:
Now, anytime you make a change of any kind to your sheet, the data will autosort. If that doesn't work, post up your final code and we'll help you "tweak" it.Code:Private Sub Worksheet_Change(ByVal Target As Range) paste your code here End Sub
When you post code, be sure to GO ADVANCED, past the code into the forum, highlight it, then click on the # icon to wrap it on code tags so that it is readable.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thanks for the info, i'll try it tonight.
Quick question though, where exactly do I turn on the Macro Recorder?
Thanks!
Tools > Macros > Record a new macro
In Excel, pressing F1 will popup the built-in help menu.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
This is what I have! The first part, at top, is for my pivot tables! Any ideas?
Thanks
Code:Option Explicit Private Sub Worksheet_Activate() ActiveSheet.PivotTables("TOTAL").PivotCache.Refresh End Sub Private Sub Worksheet_Change(ByVal Target As Range) Sort1() ' ' Sort1 Macro ' ' Application.Goto Reference:="R1C1:R10000C2" End Sub
Last edited by THORmx; 04-27-2009 at 07:27 AM.
anyone? Please help![]()
I don't see your question....just an "any ideas" query. That's not as helpful as you would think.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
True. You asked me to "paste" my final code if it needed tweaking. Well, I posted it as it's not working, and I'm getting a debugging error...
I kind of have this working now, however I'm having a few hiccups. It seems that it will automate before I completely finish typing. The document has 2 columns of data that need to move together. Basically, column A has a name, and B is data that supports it. I need them to both move.
The automate works great except for 1 hitch......as soon as I type something into Column A and hit enter, it auto sorts. I need to type data into column B too and hit enter, before it decides to automate. Once that happens, it'll be PERFECT!!
Thanks again JBeaucaire for the help so far, it's appreciated!!
Last edited by THORmx; 05-23-2009 at 08:10 AM.
Just to save time, can you post the sheet you're working on so I can tweak it directly?
GO ADVANCED > paperclip icon.
Thanks.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Here JB.
It's a list of DVD's. Thanks for your time!
You just need the worksheet code to evaluate BOTH cells before it actually moves anything, so I added like so:
Code:Option Explicit Private Sub Worksheet_Activate() ActiveSheet.PivotTables("HI2").PivotCache.Refresh End Sub Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("A:B")) Is Nothing Then _ If Cells(Target.Row, "A") <> "" And Cells(Target.Row, "B") <> "" Then _ Columns("A:B").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thanks JB, seems to work great! One last thing to ask...is it possible to have it re order when I delete something out of the 2 cells? Like lets say I have "taken" in A5, and "DVD" in B5, and I delete them. Anyway to re sort when I delete things?
Thanks again so much!
OK, try this:
Code:Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("A:B")) Is Nothing Then _ If (Cells(Target.Row, "A") <> "" And Cells(Target.Row, "B") <> "") Or _ (Cells(Target.Row, "A") = "" And Cells(Target.Row, "B") = "") Then _ Columns("A:B").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Seems to work like a CHARM JB. Thanks so much. Sorry for not responding earlier, my laptop crashed!!
Thanks again
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks