+ Reply to Thread
Results 1 to 6 of 6

comboboxs in a userform handling a protected workbook and worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    01-05-2014
    Location
    London, England
    MS-Off Ver
    Excel 20010
    Posts
    28

    comboboxs in a userform handling a protected workbook and worksheets

    I have created a userform1 with two dependant combo boxes, the worksheets and workbook is protected and I am struggling to place the unprotect function in the correct place. The purpose is to select from combo box one then select from combo box two; the value in combo box two is used by a command button on the user form to open the desired worksheet. Here lies the problem when everything is protected.
    Location is combo box one and sheet name is combo box two.

    The code functions without protection on.

    Private Sub ComboBox1_Change()
        ThisWorkbook.Unprotect Password:="password"
    Dim index As Integer
    index = ComboBox1.ListIndex
    Me.ComboBox2.Clear
    
    Select Case index
        Case Is = 0
             With ComboBox2
                 .AddItem "sheet one"
             End With
             
        Case Is = 1
             With ComboBox2
                 .AddItem "sheet two"
                 .AddItem "sheet three"
             End With
             
        Case Is = 2
             With ComboBox2
                 .AddItem "sheet four"
                 .AddItem "sheet five"
             End With
             
        Case Is = 3
             With ComboBox2
                 .AddItem "sheet six"
             End With
             
         Case Is = 4
             With ComboBox2
                 .AddItem "sheet seven"
                 .AddItem "sheet eight"
                 .AddItem "sheet nine"
             End With
    End Select
    End Sub
    
    Private Sub CommandButton1_Click()
        ThisWorkbook.Unprotect Password:="password"
          Dim actWsh As String
        If ComboBox1.Value = "" Or _
           ComboBox2.Value = "" Then
            MsgBox "Both comboboxes must have a selection."
            Exit Sub
        Else
            MsgBox "OK to proceed."
            actWsh = ComboBox2.Text
            Worksheets(actWsh).Select
        Unload UserForm1
        End If
        ThisWorkbook.Protect Password:="password"
    End Sub
    
    
    Private Sub UserForm_Initialize()
        ThisWorkbook.Unprotect Password:="password"
    With ComboBox1
         .AddItem "Location A"
         .AddItem "Location B"
         .AddItem "Location C"
         .AddItem "Location D"
         .AddItem "Location E"
    End With
    End Sub

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: comboboxs in a userform handling a protected workbook and worksheets

    Could you attach a sample of the workbook with the userform and code you are using? I noticed that when the userform initializes, it unprotects the workbook. This could be why it's working because it has already been unprotected when the userform shows.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    01-05-2014
    Location
    London, England
    MS-Off Ver
    Excel 20010
    Posts
    28

    Re: comboboxs in a userform handling a protected workbook and worksheets

    The issue is relating to "hidden" and not password related. The code is fully functional for both protected and unprotected workbook and worksheets.
    The problem relates to [code] actWsh = ComboBox2.Text Worksheets(actWsh).Select [code] obviously the worksheet has to be unhidden for actWsh (active worksheet) to function.
    I believe I've uploaded a representative file. password = password
    Attached Files Attached Files

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: comboboxs in a userform handling a protected workbook and worksheets

    Hi there,

    See if the attached version of your workbook does what you need. It uses the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub UserForm_Initialize()
    
        With Me.ComboBox1
    
            .AddItem "Northampton"
            .AddItem "Walton"
            .AddItem "Hamble"
            .AddItem "Kingsbury"
            .AddItem "Isle of Grain"
    
        End With
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub ComboBox1_Change()
    
        Dim index As Integer
    
        index = ComboBox1.ListIndex
    
        With Me.ComboBox2
    
            .Clear
    
            Select Case index
    
                   Case 0
    
                        .AddItem "Northampton Terminal"
             
                   Case 1
    
                        .AddItem "Walton Terminal"
                        .AddItem "Walton Weir"
    
                   Case 2
    
                        .AddItem "Hamble Terminal"
                        .AddItem "Hamble Jetty"
    
                   Case 3
    
                        .AddItem "Kingsbury Terminal"
    
                   Case 4
    
                       .AddItem "IOG Yard Area"
                       .AddItem "IOG Non Jet A-1 Storage"
                       .AddItem "IOG Jetty"
    
            End Select
    
        End With
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CommandButton1_Click()
    
        Const sPASSWORD     As String = "password"
    
        Dim sSheetToShow    As String
        Dim wks             As Worksheet
    
        If Me.ComboBox1.Value <> vbNullString And _
           Me.ComboBox2.Value <> vbNullString Then
    
              MsgBox "OK to proceed."
    
              sSheetToShow = Me.ComboBox2.Text
    
              With ThisWorkbook
    
                  .Unprotect Password:=sPASSWORD
    
                      For Each wks In ThisWorkbook.Worksheets
    
                          If wks.Name <> "Index" Then
                              wks.Visible = xlSheetHidden
                          End If
    
                      Next wks
    
                      .Worksheets(sSheetToShow).Visible = xlSheetVisible
    
                      .Worksheets(sSheetToShow).Activate
            
                  .Protect Password:=sPASSWORD
    
              End With
    
              Unload Me
    
        Else: MsgBox "Both comboboxes must have a selection."
    
        End If
    
    End Sub

    Selecting a new sheet via the ComboBoxes automatically hides whichever "target" worksheet was selected previously.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-05-2014
    Location
    London, England
    MS-Off Ver
    Excel 20010
    Posts
    28

    Re: comboboxs in a userform handling a protected workbook and worksheets

    Remove the code ActWsh.Visible=xlsheetvisble and replace it with Worksheets(ActWsh).Visible=xlSheetVisible Fixed the code

  6. #6
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: comboboxs in a userform handling a protected workbook and worksheets

    Glad you were able to get it working.

+ 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. [SOLVED] Userform as printer interface for a protected workbook.
    By moosetales in forum Excel Programming / VBA / Macros
    Replies: 40
    Last Post: 04-27-2014, 08:43 PM
  2. populate comboboxs on userform
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-22-2013, 02:57 PM
  3. [SOLVED] Dependent Dynamic Lists for Userform Comboboxs
    By bharbir in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-22-2013, 01:58 AM
  4. [SOLVED] two userform comboboxs depend each other by value
    By hassan khansa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2013, 05:56 AM
  5. Userform with comboBoxs acting as filters
    By Rhamilton2512 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-13-2012, 05:49 PM
  6. Way to merge 3 userform comboboxs' data into a single cell, such as a date?
    By smaier69 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2012, 09:47 PM
  7. [SOLVED] Group, Outline, Protected Worksheets and Protected Workbook
    By BEEJAY in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-13-2006, 04:45 PM

Tags for this Thread

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