+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Auto sorting?

  1. #1
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Auto sorting?

    Hi,

    I was wondering if there is a way to auto sort columns in a document.

    Thanks in advance!

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Auto sorting?

    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:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range) 
    
    paste your code here
    
    End Sub
    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.

    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 the icon 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!)

  3. #3
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    Thanks for the info, i'll try it tonight.

    Quick question though, where exactly do I turn on the Macro Recorder?

    Thanks!

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Auto sorting?

    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 the icon 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!)

  5. #5
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    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.

  6. #6
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    anyone? Please help

  7. #7
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Auto sorting?

    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 the icon 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!)

  8. #8
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    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...

  9. #9
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    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.

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Auto sorting?

    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 the icon 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!)

  11. #11
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    Here JB.

    It's a list of DVD's. Thanks for your time!
    Attached Files Attached Files

  12. #12
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Auto sorting?

    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
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon 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!)

  13. #13
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    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!

  14. #14
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Auto sorting?

    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 the icon 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!)

  15. #15
    Banned User!
    Join Date
    07-07-2008
    Location
    Hawaii
    MS-Off Ver
    2007
    Posts
    42

    Re: Auto sorting?

    Seems to work like a CHARM JB. Thanks so much. Sorry for not responding earlier, my laptop crashed!!

    Thanks again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0