+ Reply to Thread
Results 1 to 14 of 14

Excel 2016 AutoSort VBA Help!

  1. #1
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Excel 2016 AutoSort VBA Help!

    Hello!

    I am having difficulties getting an old autosort code running again. I keep getting an error (end of statement) on each of the commas at the end of each line. I am simply trying to sort a data sheet (A1:T5000) by the values in Column F and then by the values in Column G. Maybe there is an easier way to have this done? Please let me know!

    Sub SortMyReport()

    Range(“A1”).CurrentRegion.Sort _
    Key1:=Range(“F1”), _
    Order1:=XLDescending, _
    Key2:=Range(“G1”), _
    Order2:=XLDescending, _
    Header:=XLYes
    End Sub

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    Hi and welcome to the forum

    All I see wrong with your posted code is
    1. that you didn't use code tags (which has nothing to do with it not working, but please read the forum rules)
    2. The style of the double quotes would generate an error....
    You have “F1” and “G1” instead of "F1" and "G1" (notice they are a bit different?)
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    This ran on my 2016 without errors:

    Please Login or Register  to view this content.
    The comma's look funny on your posted code (may just a a font thing though), but they did not cause my excel an issue, however, try copying my code and pasting it into your VBA. The quotes definitely caused issues.
    Last edited by Arkadi; 08-08-2019 at 02:10 PM.

  4. #4
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: Excel 2016 AutoSort VBA Help!

    Hmm.. The code finally went through but it did not work. Let me give a bit more background information. I am trying to complete this multi-level sort on "sheet1" of a 5 sheet excel file (not sure if I need to specify the sheet name). Second when the code ran it did not sort how I had thought it would have. The table is the entire sheet (A1:T5000) with Row 1 being the column headings. I am looking to sort Column F and then Column G with this code. And like I said the above code did not work. Any suggestions?

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    Not sure if sheet1 was the active sheet?

    The intention is that Column F takes precendence correct? What I mean is that G will be sub-sorted without affecting the order of F?

    If the sheet is not active then it will sort a different one, because the code doesn't specify it. So add that for starters.

    Please Login or Register  to view this content.
    If possible attach a small data sample with expected sort results. To attach a file:

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

  6. #6
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: Excel 2016 AutoSort VBA Help!

    Here is the file that shows the raw data on tab 1 and then the sorted data on tab 2. The screenshot shows the sorting that I want to have done but just automatically with VBA. Keep in mind that I am sorting on descending and DEPENDS will become VERIFY so that it will sort correctly. So Column F would take precedence to Column G. Thanks!!

    AutoSort.PNG

    Auto Sort File.xlsx

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    Used my code from post 5 on the sample file you sent, and the sorting of "Raw Data" ends up identical to "Sorted"

    However, YES, DEPENDS, NO... I assume that is a custom list? Perhaps that is the problem, I'm purely sorting on values
    Last edited by Arkadi; 08-08-2019 at 03:24 PM.

  8. #8
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: Excel 2016 AutoSort VBA Help!

    Still does not work. It could be because the sheet I am trying to sort has hlookup formulas to pull data from an "original report dump" tab. Would that interfere? Like you said it works great on the sample but not the actual document. Another thing too could be that the hlookup formulas go from A2:T5000 but the data that gets "imported" has a 'random' end row meaning that it does not fulfill the entire document. There is #N/A's from around the 3500th row to 5000th.

  9. #9
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: Excel 2016 AutoSort VBA Help!

    I just figured out the issue. The Hlookups had ROW() as the row index # which was changing when the data was being sorted. I'm solving that by hardcoding the row#s into the formula to then make the formula static instead of dynamic depending on the row that it is in.

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    Oh ok, great
    Last edited by Arkadi; 08-09-2019 at 10:41 AM.

  11. #11
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: Excel 2016 AutoSort VBA Help!

    No, the sort columns are vlookups. Hence why the sorting is not working due to the vlookups not being dependent on the row that it is within while the hlookups with ROW() is. The Hlookups provide the data movement from the original to edited tab while the vlookups provide a "answer" to the question in the column due to the data pulled from the hlookups. Hopefully that makes sense lol .

  12. #12
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    This may also help: https://contexturesblog.com/archives...rting-problem/

    Applies to vlookup but hlookup may behave the same way.

  13. #13
    Registered User
    Join Date
    08-08-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: Excel 2016 AutoSort VBA Help!

    I got it to finally work! It was the "ROW()" portion of the hlookup formula. The last thing I can think of is how to make this code automatically run when a cell value changes. Thank you so much for your help!!

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel 2016 AutoSort VBA Help!

    To make it auto update, you would want to change this to a worksheet change event. But... change events only fire if you actually change something on the sheet, not if the change is the result of a formula.

+ 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] No value given for one or more parameters - Access 2016 - Excel 2016
    By schulzy175 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-18-2018, 10:11 PM
  2. Replies: 1
    Last Post: 09-18-2017, 08:27 AM
  3. [SOLVED] Issue send email with excel 2016 true outlook 2016
    By kirana2014 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-12-2017, 11:34 AM
  4. Help! Automated Email coding for Excel 2016 and Outlook 2016 problems
    By lisa6421 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-01-2017, 08:07 AM
  5. Replies: 2
    Last Post: 05-24-2016, 10:43 PM
  6. Autosort excel, complex sorting
    By Here2learn in forum Excel General
    Replies: 2
    Last Post: 12-16-2015, 05:19 AM
  7. Excel 2010 autosort
    By wbromerz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-31-2011, 10:28 AM

Tags for this Thread

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