+ Reply to Thread
Results 1 to 5 of 5

Do Macros still perform a function even if that function is already set in an Excel file?

  1. #1
    Registered User
    Join Date
    01-15-2013
    Location
    USA
    MS-Off Ver
    2010
    Posts
    40

    Question Do Macros still perform a function even if that function is already set in an Excel file?

    Sorry for the confusing thread title. Here is an example.

    Let's say I program a macro to turn filters on in the header row. But then I run that macro in a file that already has filter on in the header row. In that case, will the macro then turn off the filter?

    Another example:

    Let's say I program the macro to unwrap the text, then I run the macro in a file where the text is already unwrapped. Will it then end up wrapping the text instead?

  2. #2
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Do Macros still perform a function even if that function is already set in an Excel fi

    That depends on the command used in the macro. Most of the time, the commands perform a specific function, this command is to only turn on something or this command is only used to turn off. Depending on the command sometimes its okay to be redundant and will not break the code, but sometimes there is a preset limit and if you try to do something that is already on it will give you an error.

  3. #3
    Registered User
    Join Date
    01-15-2013
    Location
    USA
    MS-Off Ver
    2010
    Posts
    40

    Re: Do Macros still perform a function even if that function is already set in an Excel fi

    So in these examples, I am performing the command from the ribbon at the top. For filter, I go to the Data tab and click the Filter button. For unwrapping text, I am going to the Home tab and clicking the Wrap Text button.

  4. #4
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Do Macros still perform a function even if that function is already set in an Excel fi

    off the top of my head i know that filter that on and off are separate.

    The command is not the same as the button you press. The easiest thing to do is to press record macro and click a button that toggles between something. Then stop the macro record and look at the code, you see if the command the macro picked up repeats in that case it does toggle and you can use the same command but if you see two different commands then you would know that the button actually does 2 different things.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,761

    Re: Do Macros still perform a function even if that function is already set in an Excel fi

    For the ribbon buttons that have "on" and "off" states, Excel keeps track of those states and that is taken into account when the macro recorder generates code, but the code specifically turns the attribute on or off. The code does not say, "simulate the press of the button". It says, "Oh, the button is on, so I'm going to generate code that turns it off."

    For example, for wrap text, there is a specific cell attribute called WrapText that must be set to True or False. If the button is off, and you record a macro while pressing the button, the code will set WrapText to True. The macro record does not generate code that says, "Do whatever would happen if I pressed the button." So if text is already wrapped and you execute code to set WrapText to True, it will have no effect. If you want to write code that toggles it, that is, acts like the button, then you want

    Please Login or Register  to view this content.
    This is basically how the button behaves but you will never be able to generate that code with the macro recorder--you have to write it.

    Similarly, for filter, VBA sets up very specific filtering criteria, so it is "turn filtering on". Turning back off requires a simpler but different set of code. So it's not just a toggle. You could write code that acts just like the Filter button, but you can't get that just by turning on the macro recorder and clicking the Filter button.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. perform 1 function, stop, perform different function
    By nastech in forum Excel General
    Replies: 0
    Last Post: 08-22-2006, 07:25 AM
  2. [SOLVED] How to perform the following function in Excel?
    By Eric in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-05-2006, 10:25 AM
  3. [SOLVED] How do I perform an IF function on Microsoft excel?
    By katlawlor in forum Excel General
    Replies: 2
    Last Post: 02-20-2006, 02:10 AM
  4. [SOLVED] Perform the square root function in excel.
    By Se7en in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. Perform the square root function in excel.
    By Se7en in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM

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