VBA custom password protecting for more than 1 sheet












0














I'm currently working on a macro enabled excel sheet, with multiple tabs (there are 9 tabs I would like to do this on, but for the purposes of the question I'll include only 2), and for each tab I would like to add a password prompt that matches what I specify in the code.



This is working ok, but my issue is that when two sheets are located next to each other on the actual worksheets tab, it will go through them both rather than hiding the first one until i input the correct password.



For example, on my sheet I have a tab named Cascada, followed by a tab named Cascada2. If I were to put a blank tab inbetween these two, then my code would work correctly. However when they are in sequence, it seems to go through the sequence of password prompts regardless of whether I input the correct string or not.



See code below, any advice would be appreciated.



Thanks.



EDIT UPDATED WITH ANSWER



Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Application.EnableEvents = False

Dim cascada As String, cascada2 As String
cascada = "Config_Cascada"
Rhea = "Config_Rhea"

Select Case Sh.Name
Case cascada, cascada2

Dim pwd As String
pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

Dim Response As String
Response = InputBox("Enter password to view sheet")

If Response = pwd Then
Sh.Select
Else
Worksheets("Doors").Activate
End If


End Select

Select Case Sh.Name
Case Rhea

Dim pwdRhea As String
pwdRhea = "rhea"

Dim ResponseRhea As String
ResponseRhea = InputBox("Enter password to view sheet")

If Response = pwdRhea Then
Sh.Select
Else
Worksheets("Doors").Activate
End If


End Select

Application.EnableEvents = True

End Sub









share|improve this question





























    0














    I'm currently working on a macro enabled excel sheet, with multiple tabs (there are 9 tabs I would like to do this on, but for the purposes of the question I'll include only 2), and for each tab I would like to add a password prompt that matches what I specify in the code.



    This is working ok, but my issue is that when two sheets are located next to each other on the actual worksheets tab, it will go through them both rather than hiding the first one until i input the correct password.



    For example, on my sheet I have a tab named Cascada, followed by a tab named Cascada2. If I were to put a blank tab inbetween these two, then my code would work correctly. However when they are in sequence, it seems to go through the sequence of password prompts regardless of whether I input the correct string or not.



    See code below, any advice would be appreciated.



    Thanks.



    EDIT UPDATED WITH ANSWER



    Private Sub Workbook_SheetActivate(ByVal Sh As Object)

    Application.EnableEvents = False

    Dim cascada As String, cascada2 As String
    cascada = "Config_Cascada"
    Rhea = "Config_Rhea"

    Select Case Sh.Name
    Case cascada, cascada2

    Dim pwd As String
    pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

    Dim Response As String
    Response = InputBox("Enter password to view sheet")

    If Response = pwd Then
    Sh.Select
    Else
    Worksheets("Doors").Activate
    End If


    End Select

    Select Case Sh.Name
    Case Rhea

    Dim pwdRhea As String
    pwdRhea = "rhea"

    Dim ResponseRhea As String
    ResponseRhea = InputBox("Enter password to view sheet")

    If Response = pwdRhea Then
    Sh.Select
    Else
    Worksheets("Doors").Activate
    End If


    End Select

    Application.EnableEvents = True

    End Sub









    share|improve this question



























      0












      0








      0







      I'm currently working on a macro enabled excel sheet, with multiple tabs (there are 9 tabs I would like to do this on, but for the purposes of the question I'll include only 2), and for each tab I would like to add a password prompt that matches what I specify in the code.



      This is working ok, but my issue is that when two sheets are located next to each other on the actual worksheets tab, it will go through them both rather than hiding the first one until i input the correct password.



      For example, on my sheet I have a tab named Cascada, followed by a tab named Cascada2. If I were to put a blank tab inbetween these two, then my code would work correctly. However when they are in sequence, it seems to go through the sequence of password prompts regardless of whether I input the correct string or not.



      See code below, any advice would be appreciated.



      Thanks.



      EDIT UPDATED WITH ANSWER



      Private Sub Workbook_SheetActivate(ByVal Sh As Object)

      Application.EnableEvents = False

      Dim cascada As String, cascada2 As String
      cascada = "Config_Cascada"
      Rhea = "Config_Rhea"

      Select Case Sh.Name
      Case cascada, cascada2

      Dim pwd As String
      pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

      Dim Response As String
      Response = InputBox("Enter password to view sheet")

      If Response = pwd Then
      Sh.Select
      Else
      Worksheets("Doors").Activate
      End If


      End Select

      Select Case Sh.Name
      Case Rhea

      Dim pwdRhea As String
      pwdRhea = "rhea"

      Dim ResponseRhea As String
      ResponseRhea = InputBox("Enter password to view sheet")

      If Response = pwdRhea Then
      Sh.Select
      Else
      Worksheets("Doors").Activate
      End If


      End Select

      Application.EnableEvents = True

      End Sub









      share|improve this question















      I'm currently working on a macro enabled excel sheet, with multiple tabs (there are 9 tabs I would like to do this on, but for the purposes of the question I'll include only 2), and for each tab I would like to add a password prompt that matches what I specify in the code.



      This is working ok, but my issue is that when two sheets are located next to each other on the actual worksheets tab, it will go through them both rather than hiding the first one until i input the correct password.



      For example, on my sheet I have a tab named Cascada, followed by a tab named Cascada2. If I were to put a blank tab inbetween these two, then my code would work correctly. However when they are in sequence, it seems to go through the sequence of password prompts regardless of whether I input the correct string or not.



      See code below, any advice would be appreciated.



      Thanks.



      EDIT UPDATED WITH ANSWER



      Private Sub Workbook_SheetActivate(ByVal Sh As Object)

      Application.EnableEvents = False

      Dim cascada As String, cascada2 As String
      cascada = "Config_Cascada"
      Rhea = "Config_Rhea"

      Select Case Sh.Name
      Case cascada, cascada2

      Dim pwd As String
      pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

      Dim Response As String
      Response = InputBox("Enter password to view sheet")

      If Response = pwd Then
      Sh.Select
      Else
      Worksheets("Doors").Activate
      End If


      End Select

      Select Case Sh.Name
      Case Rhea

      Dim pwdRhea As String
      pwdRhea = "rhea"

      Dim ResponseRhea As String
      ResponseRhea = InputBox("Enter password to view sheet")

      If Response = pwdRhea Then
      Sh.Select
      Else
      Worksheets("Doors").Activate
      End If


      End Select

      Application.EnableEvents = True

      End Sub






      excel vba excel-vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 14:58







      Macca424

















      asked Nov 20 '18 at 16:50









      Macca424Macca424

      305




      305
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Give this a shot. Cleaner and works as far as I tested:



          Private Sub Workbook_SheetActivate(ByVal Sh As Object)

          Application.EnableEvents = False

          Dim cascada As String, cascada2 As String
          cascada = "config_Cascada"
          cascada2 = "config_Cascada2"

          Select Case Sh.Name
          Case cascada, cascada2

          Dim pwd As String
          pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

          Dim Response as String
          Response = InputBox("Enter password to view sheet")

          If Response = pwd Then
          Sh.Select
          End If

          End Select

          Application.EnableEvents = True

          End Sub





          share|improve this answer























          • Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
            – Macca424
            Nov 21 '18 at 10:21








          • 1




            see now. i made a small change. there's no need to hide/unhide the sheet
            – Scott Holtzman
            Nov 21 '18 at 17:55










          • Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
            – Macca424
            Nov 22 '18 at 14:50












          • I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
            – Macca424
            Nov 22 '18 at 14:57










          • @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
            – Scott Holtzman
            Nov 23 '18 at 1:11











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397769%2fvba-custom-password-protecting-for-more-than-1-sheet%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Give this a shot. Cleaner and works as far as I tested:



          Private Sub Workbook_SheetActivate(ByVal Sh As Object)

          Application.EnableEvents = False

          Dim cascada As String, cascada2 As String
          cascada = "config_Cascada"
          cascada2 = "config_Cascada2"

          Select Case Sh.Name
          Case cascada, cascada2

          Dim pwd As String
          pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

          Dim Response as String
          Response = InputBox("Enter password to view sheet")

          If Response = pwd Then
          Sh.Select
          End If

          End Select

          Application.EnableEvents = True

          End Sub





          share|improve this answer























          • Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
            – Macca424
            Nov 21 '18 at 10:21








          • 1




            see now. i made a small change. there's no need to hide/unhide the sheet
            – Scott Holtzman
            Nov 21 '18 at 17:55










          • Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
            – Macca424
            Nov 22 '18 at 14:50












          • I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
            – Macca424
            Nov 22 '18 at 14:57










          • @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
            – Scott Holtzman
            Nov 23 '18 at 1:11
















          2














          Give this a shot. Cleaner and works as far as I tested:



          Private Sub Workbook_SheetActivate(ByVal Sh As Object)

          Application.EnableEvents = False

          Dim cascada As String, cascada2 As String
          cascada = "config_Cascada"
          cascada2 = "config_Cascada2"

          Select Case Sh.Name
          Case cascada, cascada2

          Dim pwd As String
          pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

          Dim Response as String
          Response = InputBox("Enter password to view sheet")

          If Response = pwd Then
          Sh.Select
          End If

          End Select

          Application.EnableEvents = True

          End Sub





          share|improve this answer























          • Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
            – Macca424
            Nov 21 '18 at 10:21








          • 1




            see now. i made a small change. there's no need to hide/unhide the sheet
            – Scott Holtzman
            Nov 21 '18 at 17:55










          • Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
            – Macca424
            Nov 22 '18 at 14:50












          • I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
            – Macca424
            Nov 22 '18 at 14:57










          • @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
            – Scott Holtzman
            Nov 23 '18 at 1:11














          2












          2








          2






          Give this a shot. Cleaner and works as far as I tested:



          Private Sub Workbook_SheetActivate(ByVal Sh As Object)

          Application.EnableEvents = False

          Dim cascada As String, cascada2 As String
          cascada = "config_Cascada"
          cascada2 = "config_Cascada2"

          Select Case Sh.Name
          Case cascada, cascada2

          Dim pwd As String
          pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

          Dim Response as String
          Response = InputBox("Enter password to view sheet")

          If Response = pwd Then
          Sh.Select
          End If

          End Select

          Application.EnableEvents = True

          End Sub





          share|improve this answer














          Give this a shot. Cleaner and works as far as I tested:



          Private Sub Workbook_SheetActivate(ByVal Sh As Object)

          Application.EnableEvents = False

          Dim cascada As String, cascada2 As String
          cascada = "config_Cascada"
          cascada2 = "config_Cascada2"

          Select Case Sh.Name
          Case cascada, cascada2

          Dim pwd As String
          pwd = "cascada" & IIf(Sh.Name = cascada2, 2, "")

          Dim Response as String
          Response = InputBox("Enter password to view sheet")

          If Response = pwd Then
          Sh.Select
          End If

          End Select

          Application.EnableEvents = True

          End Sub






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 17:55

























          answered Nov 20 '18 at 17:17









          Scott HoltzmanScott Holtzman

          22.9k62748




          22.9k62748












          • Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
            – Macca424
            Nov 21 '18 at 10:21








          • 1




            see now. i made a small change. there's no need to hide/unhide the sheet
            – Scott Holtzman
            Nov 21 '18 at 17:55










          • Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
            – Macca424
            Nov 22 '18 at 14:50












          • I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
            – Macca424
            Nov 22 '18 at 14:57










          • @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
            – Scott Holtzman
            Nov 23 '18 at 1:11


















          • Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
            – Macca424
            Nov 21 '18 at 10:21








          • 1




            see now. i made a small change. there's no need to hide/unhide the sheet
            – Scott Holtzman
            Nov 21 '18 at 17:55










          • Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
            – Macca424
            Nov 22 '18 at 14:50












          • I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
            – Macca424
            Nov 22 '18 at 14:57










          • @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
            – Scott Holtzman
            Nov 23 '18 at 1:11
















          Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
          – Macca424
          Nov 21 '18 at 10:21






          Thanks for the reply, your code works perfectly when putting in the right password, however if you put in the wrong password it seems to delete the sheet entirely, even when closing and reopening, it is still not there. I've had to load a backup and test it a few times. I also would like to scale this to the 9 or so sheets I have in the document, which won't all contain the string cascada in them. Thanks.
          – Macca424
          Nov 21 '18 at 10:21






          1




          1




          see now. i made a small change. there's no need to hide/unhide the sheet
          – Scott Holtzman
          Nov 21 '18 at 17:55




          see now. i made a small change. there's no need to hide/unhide the sheet
          – Scott Holtzman
          Nov 21 '18 at 17:55












          Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
          – Macca424
          Nov 22 '18 at 14:50






          Ok thanks, see edited OP. I added one piece to your code which redirects me to a "home" page if they put in the incorrect password, the last thing i want to do now is scale this to the 9 or so sheets i mentionned in my opening post. For example I could have another tab called "config_KitDrws" which I want the password to be "KitDrws" for. What would be the best way to do this? To separate out my case statements and add each password that I want?
          – Macca424
          Nov 22 '18 at 14:50














          I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
          – Macca424
          Nov 22 '18 at 14:57




          I answered myself a bit, see OP for solution to this, not sure if it's best practice but it works well.
          – Macca424
          Nov 22 '18 at 14:57












          @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
          – Scott Holtzman
          Nov 23 '18 at 1:11




          @Macca424 - by doing what you did you butchered the entire SO experience. You asked a question then edit your question with your own answer. That is not at all helpful to someone coming behind you. On top of that you added features to the original question, thus essentially, adding a question. Lastly, if the answer provided answered your question, it's best practice to mark it as answered.
          – Scott Holtzman
          Nov 23 '18 at 1:11


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397769%2fvba-custom-password-protecting-for-more-than-1-sheet%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'