+ Reply to Thread
Results 1 to 28 of 28

Need help with VBA for automatic sorting

  1. #1
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Need help with VBA for automatic sorting

    Hi, I work for a pottery and run the tool room. I keep a log of all the parts we order and keep in stock in an excel spreadsheet. I have it sorted so that all the newest purchase orders are at the top and go down. Im wanting a code that will automatically do this if I add a new record. I have watched some tutorials but they show just 2 columns. I have 13 columns and need the code to sort by column 9 but move all the info from all 13 columns. I also have row 1 as a header. Can anybody help make me a vba code for this or somehow explain to me how to do it?

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    Welcome to the forum

    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Sorry new to all this. Don't post on forums much but i believe I uploaded it correctly.
    Attached Files Attached Files

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    The code to sort is trivial, but just want to understand your workflow a little better...

    You say you want this to happen automatically when a new record is added - but what's the trigger for a new record?
    - Is it when ANY value is added to a new row, below existing data?
    - Is it when a specific field is populated (Date Ordered?)
    - Is it when ALL fields are populated (but not all fields are completed for all rows, in existing data) ?

    When multiple rows have the same Date Ordered, what's the secondary sort key (which field should be used to sort next) ?

  5. #5
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    I want it to sort when the purchase order column is changed. So if I add a new record it moves it to the top once i put the PO in. and if i update a record with a new PO number it will automatically move it as well. so that its always keeping the most recent PO at the top

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    Something like this, in the worksheet module:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Wow thank you but I was hoping to have it sorting by the purchase order column. can i just change the parts of the code from "Date Ordered" to "P.O. Number"

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    My misread. Yes, change both instances of "DATE ORDERED" to "P.O. NUMBER" (or whichever column header you wish to sort by).

  9. #9
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Awesome thank you so much for all the help

  10. #10
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    I copied that and put it into the code area but nothing is changing if i add or change anything.

  11. #11
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Need help with VBA for automatic sorting

    is the code in the worksheet module and not a normal module
    Please Login or Register  to view this content.
    right click on the sheet tab and click "View Code"

  12. #12
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Yes it is in a worksheet module

  13. #13
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    See attached worked example.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    I downloaded yours. Opened it. it worked perfectly. I copied the code. Put it into mine and nothing.

  15. #15
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    Attach the workbook where it doesn't work

  16. #16
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Im sorry but what do you mean? I not sure know how to do that.

  17. #17
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  18. #18
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Ok sorry I didnt read that correctly. This is my file ive been working on the sheet at the end just to try it out, The sheet labeled INVENTORY is where i will ultimately use it.
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Any chance you were able to figure it out. Sorry Ive been super busy at work and havent gotten to get on here and check but im still having trouble with this.

  20. #20
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    The codename for your sheet named "Sheet1" is Sheet7. So this line needs to change:

    Please Login or Register  to view this content.
    We don't actually have to qualify this reference as we're calling it from the worksheet change event, so this line can read simply:

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    That worked perfectly! Thank you!

  22. #22
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    I lied. Worked fine in my sample sheet, however when i tried putting it into my inventory sheet its says there is an error. Is it because i already have another code working in that sheet?

  23. #23
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    What is "an error" ?

  24. #24
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    it says
    Compile Error:


    Ambiguous name detected: Worksheet_Change

  25. #25
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    You can only have one Worksheet_Change subroutine per sheet. If you already have a Worksheet_Change sub, you will have to integrate this code into that sub.

  26. #26
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    Could you possibly tell me how to do that without messing anything up?

  27. #27
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with VBA for automatic sorting

    Something like this:

    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    01-10-2020
    Location
    East Liverpool, OH
    MS-Off Ver
    Office 2010
    Posts
    16

    Re: Need help with VBA for automatic sorting

    work absolutely perfectly thank you so much. Im sorry to have been so needy.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Automatic sorting
    By bodi22577 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-18-2015, 12:04 PM
  2. Automatic sorting
    By chris.slater in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-02-2013, 08:42 AM
  3. Automatic Sorting
    By buddy123 in forum Excel General
    Replies: 3
    Last Post: 09-05-2012, 09:18 AM
  4. Automatic Sorting
    By SumTuck in forum Excel General
    Replies: 3
    Last Post: 06-22-2012, 06:58 PM
  5. Automatic sorting?
    By controlfreak in forum Excel General
    Replies: 2
    Last Post: 06-04-2009, 07:28 AM
  6. Automatic sorting
    By greg7468 in forum Excel General
    Replies: 6
    Last Post: 09-20-2006, 12:43 PM
  7. Automatic Sorting?????
    By Bigredno8 in forum Excel General
    Replies: 3
    Last Post: 05-28-2005, 06:11 PM

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.6.0 RC 1