+ Reply to Thread
Results 1 to 14 of 14

Sort a Table in Excel Instance

  1. #1
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Sort a Table in Excel Instance

    Full disclosure I posted this code on another forum, but it was for a different questions that was resolved.

    I have a piece of MS Access code that opens an excel instance. Once open Access appends new records to a table. Think most recent monthly detail. This works great. My problem is that after the data is appended, I want to sort the table in descending order. Below is my code.

    I should mention the one line of code works as expected if I just paste it into the regular excel file, (not the instance opened by MS Access)

    Please Login or Register  to view this content.
    When I run it as pasted above I get a compiler error saying "Sub or Function not defined" With the word "Range highlighted.
    I'm sure I am making this harder than it needs to be. Any help or guideline at this point would be greatly appreciated.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Sort a Table in Excel Instance

    Hi there,

    This is just a knee-jerk and untested response!


    I get a compiler error saying "Sub or Function not defined" With the word "Range highlighted
    I'm assuming that it's the SECOND instance of the word "Range" that's highlighted by the compiler - if so, try prefixing it with a period, i.e. .Range - the period will "include" it in the

    Please Login or Register  to view this content.
    block - without the period, Access VBA won't recognise the "Range" object.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M



    P. S. I'm not sure, but you might also need to use excelApp.xlDescending to ensure that Access VBA recognises the constant.
    Last edited by Greg M; 08-08-2020 at 08:57 PM. Reason: P. S. added

  3. #3
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,186

    Re: Sort a Table in Excel Instance

    @Greg M;

    Using values of the constants would be better.... i.e.;

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Sort a Table in Excel Instance

    Hi Haluk,

    Thanks for your feedback.

    That's a good approach, and is a LOT better than inserting hard-coded values directly in the command itself, i.e.:

    Please Login or Register  to view this content.
    A possible slight advantage of the approach I suggested might be that it "future-proofs" the code against any Microsoft decision to change (for whatever reason!) the values of the constants.

    Regards,

    Greg M

  5. #5
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Sort a Table in Excel Instance

    Thank you very much for taking the time to answer my question. I added the code you suggested, but am still unable to get the table to sort.
    Here is my updated code:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Sort a Table in Excel Instance

    Hi again,

    Once again this is an untested suggestion - anything else is almost impossible for me as I don't have access to your database/workbooks etc.

    I'm assuming that the fault is occurring on the line marked as "Added this line", so try:

    Please Login or Register  to view this content.

    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Sort a Table in Excel Instance

    And around in circles we go...Why not just upload a sample file jrean042 so that the members can see your actual file setup and expected result...see Top Yellow Banner
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Sort a Table in Excel Instance

    If you have a reference set to the Excel object library (we can't see your declarations, so it is unclear) then you can use:

    Please Login or Register  to view this content.
    If you don't have the reference set, then add:

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Sort a Table in Excel Instance

    Thank you everyone. I wasn't able to get it working, here is my full code. Like I stated above, the line to sort the table works in a regular excel file, but not the instance opened through MS Access. So I don't know if it has any value attaching a workbook, and it would be a ton of work to dial back the MS Access database and upload that and the corresponding excel file.

    If anyone see something wrong in my code, it would be appreciated, otherwise I will close the question because Ia m not able to provide enough detail.

    Thanks again,

    Please Login or Register  to view this content.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Sort a Table in Excel Instance

    You didn't add the xlUp constant. You also left On Error Resume Next in effect, which is never a good plan.

  11. #11
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Sort a Table in Excel Instance

    Sorry, this line should not have been commented out:
    Please Login or Register  to view this content.
    That said, I don't think I left off the "xlUp". Please correct me if I am incorrect.

    I have the "On Error Resume Next" only when creating the excel instance, would that error handler have any effect on any subsequent code?
    If I run all the code with all the error handler (aside from the on error resume next) commented out, it runs and gives the same effect. Runs, appends data, but does not do the sort the table.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Sort a Table in Excel Instance

    Yes - unless you reset error handling, an On Error Resume Next will remain in effect. I can't see this in your code:

    Please Login or Register  to view this content.
    without it, that sort line will throw an error (but you won't see it due to the On Error Resume Next) and no sort will happen.

  13. #13
    Forum Contributor
    Join Date
    11-29-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    116

    Re: Sort a Table in Excel Instance

    Good to know. Most of the research I did at the time had it written that way.
    I have rewritten it as follows, creating the object first:



    Please Login or Register  to view this content.
    This works and I have made a little progress. I am now getting a runtime error 9 on the following line:
    Please Login or Register  to view this content.
    Not sure why, but at least the issue presented itself. The on error resume next was masking this error.

    Should I replace the xlUp with "-4162" in the line:

    Please Login or Register  to view this content.

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: Sort a Table in Excel Instance

    The subscript error 9 means that either there isn't a table on that sheet, or it has a different name.

    Yes, either use the literal value or add the constant declaration line that I posted (the latter is my preference).

+ 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. Last instance of value in table column
    By xybadog in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-07-2020, 01:08 PM
  2. First instance based on information from a second table
    By Plummet in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-12-2020, 07:03 PM
  3. [SOLVED] I need add a wildcard to find more than one instance from my table
    By timmtamm in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-09-2016, 06:22 PM
  4. Replies: 4
    Last Post: 07-12-2015, 09:26 PM
  5. Replies: 3
    Last Post: 07-15-2014, 09:50 AM
  6. Search table column for 1st instance of a value
    By okmred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2013, 02:17 PM
  7. Lookup the first instance of data in a table
    By hoss88 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-23-2013, 07:22 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