Run Macro from cmd button error

  • I am having trouble running a macro that I got off this site. It just protect/unprotects all the sheets at once. I want to just have a cmd button that runs it instead of have to go up to the macrolist. People will be using this that have no idea what a macro is. I get an error that reads "Run-time error '1004': Method 'Protect' of object '_Worksheet' failed". If I just run the macro from the list, no error. I have the cmd button call the module and then the module shows the userform. When I debug it takes me to either of the lines within the if else statement, depending on if the sheet is already protected or not. Here is my code, and any help would be much appreciated.



    thanks

  • Re: Run Macro from cmd button error


    HI


    Code worked for me. Where do you have each of these pieces of Code?



    Tony

  • Re: Run Macro from cmd button error


    This is the code for Module1

    Code
    Sub ShowPass()
        UserForm1.Show
    End Sub


    UserForm1 does exist. This is the code in UserForm1


    This is the code for the command button on the the spreadsheet

    Code
    Private Sub cmdPass_Click()
       Call ShowPass
    End Sub


    Are there some sort of rules that I could have in place that I dont know about. I just got this computer from somebody else at work recently and havent yet become too familiar with it. I've tried this on my neighbors computer with no such luck. thanks for the help guys

  • Re: Run Macro from cmd button error


    i tried this on my computer at home and it does work. it must be something with the settings at my work computer.

  • Re: Run Macro from cmd button error


    Do you want the UserForm to show at the opening of the workbook? I can't see why you don't just put the code into the CommandButton

    Code
    Private Sub cmdPass_Click() 
        UserForm1.Show 
    End Sub


    Then try this alternative

  • Re: Run Macro from cmd button error


    no, I do not want it to open at startup. Lots of people use it for read-only purposes. I tried just putting the code in the command button. no such luck tho. I tried your code royUK. It did the same thing. It has to be something within the setting or the controls of excel with respect to the macros. Its very odd because if I run the macro from the list it works, but not with the command button. What is also odd is that I have other command buttons that run macros and they work fine. I dont know what to do. If I cant get it to work then I cant get it to work. What can ya do.

Participate now!

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