+ Reply to Thread
Results 1 to 7 of 7

Sort Operation Works when Macros are called manually, but not when VBA calls the Macros

  1. #1
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    124

    Sort Operation Works when Macros are called manually, but not when VBA calls the Macros

    I am implementing a Pareto chart for versions of Excel that are olcer than the 2016 version, so I am copying the Pareto values to a 'helper table' and using VBA's sort operations to order the data in descending order of frequency. And everything works fine when I manually run the macros that sort the data for two Pareto charts.

    The problem is that, when I use VBA to run the macros, the sort operation appears to not occur. However, the copy operation still occurs. Also, if I move the copy/sort commands from inside the macro to userform1, the sort operation does not occur.

    I am stymied on how the sort can work when I run the macro manually, yet fail when userform1 runs the same macros. Does anyone know how to corect this behavior so that the numbers in the below graphic are sorted into descending order?
    Untitled.png

    The file is attached.

  2. #2
    Forum Expert
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,569

    Re: Sort Operation Works when Macros are called manually, but not when VBA calls the Macro

    I cannot open the .xlsb file on my system for some reason. However, often times when the condition you describe occurs, it is because the code either does not qualify all ranges with parent sheet references or it uses AcitiveSheet for a reference and the user has a sheet different than the intended sheet active at runtrime. Similar conditions can exist with Workbooks where more than one is involved. You should makke sure all range references are properly qualified to reflect the parent worksheet and workbook, where applicable. One way to handle that is to use Object Variables to hold your Workbook.Worksheet reference, then make sure that variable is used to qualify the range objects that are used in the code. As I said, I could not view the code in your file so it is difficult to offer any other advice at this point.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, click the 'Go Advanced' button, Then click 'Manage Attachements' and follow the menu.
    Thinking I am smart and knowing I am smart are two entirely different levels of schizophrenia.

  3. #3
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    124

    Re: Sort Operation Works when Macros are called manually, but not when VBA calls the Macro

    You should makke sure all range references are properly qualified to reflect the parent worksheet and workbook, where applicable
    I think i've done this, but here is one of the two macros that work when I manaually run the macros, but which do not work when I initiate the macro with a VBA comman. Do you see any holes in my qualifiers?

    Please Login or Register  to view this content.
    I don't know what to say about you not being able to open the file. I just downloaded the file and opened it.
    Last edited by lovecolorado; 05-17-2019 at 09:54 PM.

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    25,519

    Re: Sort Operation Works when Macros are called manually, but not when VBA calls the Macro

    I agree with JLG Whiz,

    Not referring to the specific sheet has tripped me up many a time in the past. It's too easy to step through a macro when de-bugging and observing the results when you keep the sheet in question as the active sheet. But if you run the maco when the sheet is not Active or referred to directly yuo;ll get anomalies.

    Use instead

    Please Login or Register  to view this content.
    And always use VBA sheet code names rather than tab names. They're too easily changed and a macro will fail if the tab name is hard coded.
    Richard Buttrey

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    124

    Re: Sort Operation Works when Macros are called manually, but not when VBA calls the Macro

    It's too easy to step through a macro when de-bugging and observing the results when you keep the sheet in question as the active sheet.
    Face palm! That is exactly what I was doing. It now works find and I may never use activesheet again. Thank you.

  6. #6
    Forum Expert
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,569

    Re: Sort Operation Works when Macros are called manually, but not when VBA calls the Macro

    I don't know what to say about you not being able to open the file. I just downloaded the file and opened it.
    I don't know what causes it either, but it seems to happen with just about any file except .xlsx and .xlsm extensions. I have checked my blocked file settings and it should be opening files with valid file extensions from 97 onward. But I get the 'Format not compatible' message. I am beginning to wonder if it is my system security that is screwed up.

  7. #7
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    124

    Re: Sort Operation Works when Macros are called manually, but not when VBA calls the Macro

    Sorry for your difficulty.

+ Reply to Thread

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