Hide / Unhide Buttons with the help of a code

  • [xpost]


    Hello Experts
    I have this excel sheet which has 2 macro buttons GenerateXML and ClearData. The GenerateXML code runs through 6 different sheets to get the expected result. The ClearData code clears the workings done in those 6 sheets by GenerateXML. I want to place the buttons in such a way that only one button is visible and the other is hidden, one on top of other with the help of a code. When I press GenerateXML automatically ClearData must be on the top and vice versa. If the user presses the GenerateXML before ClearData then the code generates an error. To avoid this, I want to try the above way.
    I have enquired in facebook but I couldn;t get any response. Hope someone will understand and find a solution to the above.

  • You can accomplish your goal with the use of a single Command Button :

  • Logit. I am not understanding. There are 2 buttons in my sheet. Do you mean that I have to delete them and combine the codes of both the buttons in one macro and add the above as you said.

  • Quote

    Do you mean that I have to delete them


    Then ... understand the code as written in the IF / THEN statement for the single button. Place your appropriate code in the existing


  • I joined both the codes in one macro, created a new button and assigned the macro. But still I am getting a complie error - variable not defined

  • If this is the line of code :

    With Sheets("Original").Select

    The workbook does not have a sheet named Original

    There are a lot of other sheet names in your code that also do not exist in the workbook. If the sheets don't exist, you will receive an

    error code for every line where it mentions a sheet name that does not exist.

  • There are 9 sheets with different names used in the code. The sheet Original is the only sheet where the data is entered and the buttons are created. The code is running perfectly when I run the buttons inidvidually. Like I said earlier, When working on a new data, if the user presses the GenerateXML button before ClearData button the code will generate an error because the old data is not cleared.

  • THen you need to adjust your code some how so the error does not occur.

    Or you'll need to use a different approach with the buttons.

  • Then I think I will have to go with 2 buttons till I get a possible solution. Thanks for your time Logit.

  • Here is another approach :

  • Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the URL from the address bar in your browser) to the cross-post. We are here to help so help us help you!

    Read this to understand why we ask you to do this


  • Logit's suggestion is correct. Your code though is nonsense.

    • You have a sub within sub, that cannot work.
    • You select a cell then change sheet and try to change the active cell on that sheet, which will be whatever cell was already selected, your code does not ensure that the correct cell is used.

    I've replaced your button and created one button that will do what you want.

  • Royuk. I have already mentioned in my first message that it was shared in other forum where I was not able to get a solution. If you mean by sharing the same query, I always try to explain my problem as simple as possible. Regarding my query, it is not the code, which is a dummy in this case, it was about how to place the buttons on top of each other and when pressed, the pressed button goes below and the next step button comes on the top. If you had read the first post then you would have understood what I am trying to do. About your code, I can’t see the solution to my query. Sorry, if I have broken any rules.

  • I have offered you a much better solution.

    You never mentioned the code being a dummy, but it is still rubbish. Why have a sub within a sub?

    You haven't bothered reading the Forum Rules and did not mention your cross post. It was infact reported by a member who had seen it on the other Forum

  • Royuk. The code has nothing to do with the query. In a hurry, I just recorded macros (not written coed) and created 2 buttons to help understand what I was trying to do. That's all.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!