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
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.
Bookmarks