+ 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
    223

    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 JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    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, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

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

    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 - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    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

    RIP - d. 06/10/2022

    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
    223

    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 JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    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
    223

    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)

Similar Threads

  1. Help condensing macro that calls 8 macros.
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-08-2017, 11:52 AM
  2. The order of operation of Macros
    By Yoepy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-05-2016, 09:50 PM
  3. [SOLVED] Combining two macros into one simple operation.
    By MagicMan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-06-2015, 08:06 AM
  4. New to Excel macros - Looking for a little help with a loop operation
    By Pwincu20 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-24-2012, 11:40 PM
  5. Macro that calls other macros in different sheets (Mudraker)
    By mlcfexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2007, 08:56 PM
  6. Macro that calls other macros in different sheets
    By mlcfexcel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-05-2007, 07:44 AM
  7. Return codes from called macros
    By mwc0914 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-05-2006, 02:15 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