Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Add Worksheet to a combo box and activate

0

Hi Team,

I have a workbook with 25 sheets and i want to use Combo box to load, select and activate a worksheet. 

I only want to add certain sheets not all. I have divided the navigation into 4 section, so four combo boxes per sheet. I want to load each combo box with different sheets. When i click on a sheet i want it to unhide,activate that sheet. The sheet to be opened will be protected, will probably need to unprotect first and then reprotect also select a cell E.g Cell B13

Can you help?

See attached file,
I can create the Active X combo boxes and load with sheet names
What i want to do is load the sheets that are described on the Document control Sheet. Some have 3 some have 2 and so on. 
What is the best method to load the sheets
On save and re open how to load the sheets automatically

Thanks in advance

Answer
Discuss

Discussion

Hi again Semaj

I'm prettty sure I could help nut you need to help me (and others) by up[loading the file you have created (including "navigation" as you metion above).

Also you mention that sheets will be protected- please set the password to something simple (per sheet?) and tell us (otherwise we can't unprotect).

Once you have a solution, you can set a proper password (and edit the code to suit).
John_Ru (rep: 6652) Mar 27, '25 at 8:23 am
thanks John, i will do that tomorrow or over the weekend and upload. What I will do is set the password to nothing. Protect and unprotect should work easily with no password. 1.18 am here now.
semaj (rep: 10) Mar 27, '25 at 10:19 am
Okay Semaj but suggest you set it to "*" (asterisk) instead. It's 14:39 here currently.
John_Ru (rep: 6652) Mar 27, '25 at 10:40 am
Hi Semaj. Please remember to upload a representative Excel file. 
John_Ru (rep: 6652) Mar 31, '25 at 5:20 am
Hi John,
I have not forgotten, i did some work yesterday to get it ready, Unfortunately work has got me completing some schedules for new projects, had to put it to one side. See how i am for time tomorrow. Appreciate the nudge
semaj (rep: 10) Mar 31, '25 at 5:42 am
Hi Semaj ,
When i click on a sheet i want it to unhide
what about hide ?
what's the condition?
Ali om Apr 1, '25 at 3:13 pm
Hi Ali om, yes there will be a hide condition, i am ok with that part. hope to upload my workbook this week, workload permitting.
semaj (rep: 10) Apr 1, '25 at 4:28 pm
Semaj. Please see my new Answer (and demo file). Note that I'm not keen of your approach of copying 8 ComboBoxes to all sheets but leave you to pursue that is you think it best.
John_Ru (rep: 6652) Apr 2, '25 at 5:43 pm
Add to Discussion

Answers

0

@Semaj

to according your requirements  here is my try and read comments inside code carefully  to adaption with your needs .

CPrivate Sub ComboBox1_Change()
    Sheets(ComboBox1.List(ComboBox1.ListIndex, 0)).Visible = xlSheetVisible
 
    Sheets(ComboBox1.List(ComboBox1.ListIndex, 0)).Activate
    Sheets(ComboBox1.List(ComboBox1.ListIndex, 0)).Unprotect Password:="123" 'change your real password
    Range("B13").Select
    Sheets(ComboBox1.List(ComboBox1.ListIndex, 0)).EnableSelection = xlNoRestrictions
 
    Sheets(ComboBox1.List(ComboBox1.ListIndex, 0)).Protect Password:="123" 'confirm  your real password again
    'do it for the others comboboxes(2,3,..)
 
End Sub
 
Private Sub UserForm_Initialize()
  Dim ws, sh, sh1, sh2 As Variant
  For Each ws In Array("PAGE 0", "PAGE 1") ' put sheets names in array
  ComboBox1.AddItem ws
  
  Next ws
  For Each sh In Array("PAGE 3", "PAGE 4") 'put sheets names in array
  ComboBox2.AddItem sh
  Next sh
  For Each sh1 In Array("RC", "AB") 'put sheets names in array
  ComboBox3.AddItem sh1
  Next sh1
  For Each sh2 In Array("PAGE 2", "PAGE 2") 'put sheets names in array
   ComboBox4.AddItem sh2
  Next sh2
  
End Sub
Discuss

Discussion

I have uploaded a files for you and others to view
Thanks in advance
semaj (rep: 10) Apr 2, '25 at 5:24 am
Just test what I gave you , because your explenation is really poor , and I don't know what exacly are sheets should be in each combobox!
I suggest for you to  use form . I don't expect use comboboxes inside sheet.
this is because of you don't clarify  that from beginning .
good luck.
Ali om Apr 2, '25 at 12:06 pm
Ali om, i uploaded a spreadsheet last night my time, on the first sheet it shows which Active X combo box has what sheet. The issue i am having, when i created the load sheet to combo box, it did not load the required sheets (if i wanted 3 sheets loaded it would load 2) and when i save the workbook and re open, i could not get the sheet to load. (Load on Open). Does that help?
semaj (rep: 10) Apr 2, '25 at 6:35 pm
check answer John maybe helps and enough for you.
Ali om Apr 3, '25 at 11:57 am
Add to Discussion
0

Semaj

In the attached file, I have a sheet called "Document Control" plus three sheets from your file (which are very hidden so must be unhid using VBA).

When the file is opened (macros-enabled), it protects and lists the three sheets, starting from cell A2 of "Document Control" using this event code (with comments):

Private Sub Workbook_Open()

    Dim ws As Worksheet, n As Integer

    myPwd = "*" ' is variable declared in Module 1

    n = 2
    Sheet1.Columns(1).ClearContents

    ' loop through sheets
    For Each ws In ThisWorkbook.Sheets

        If ws.Name <> Sheet1.Name Then
            ' add to list on DC sheet, protect and hide
            Sheet1.Cells(n, 1) = ws.Name
            ws.Protect myPwd
            ws.Visible = xlSheetVeryHidden
            ' increment row counter
            n = n + 1
        End If

    Next ws

    Sheet1.Columns(1).AutoFit

End Sub

If you click on a cell with one of the listed sheet names, that sheet is revealed and cell B13 selected (and optionally - lines in bold - written to and that sheet re-protected) using this event code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim ws As Worksheet

    ' do nothing unless a single cell in column A is picked
    If Intersect(Target, Columns(1)) Is Nothing Or Target.Count > 1 Then Exit Sub

    For Each ws In ThisWorkbook.Worksheets
        If Target.Value <> "" And ws.Name = Target.Value Then
            ' if sheet found, show with B13 selected
            ws.Visible = xlSheetVisible
            ws.Unprotect myPwd
            ws.Activate
            ws.Cells(13, 2).Select
            ' ### optional write and re-protect
            ws.Cells(13, 2).Value = ws.Name & " Activated"
            ws.Protect myPwd
        End If
    Next ws

End Sub

Hope you can modify your code to use this approach but using ComboBox selections. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Thanks John, i will test this over the next few days - and come back to you. thanks for putting in the time to look at this request and provide an answer
semaj (rep: 10) Apr 2, '25 at 6:41 pm
Okay Semaj. You could try copying all the sheets from your file (apart from Document Control) to mine then close and re-open. It should work fine - but I didn't add sheet buttons for Hide Sheet or Back to Document Control. I think I'd use a single standard module macro with a keyboard shortcut instead. 
John_Ru (rep: 6652) Apr 2, '25 at 6:58 pm
Semaj. Did you get my solution working? 
John_Ru (rep: 6652) Apr 7, '25 at 5:50 am
Not yet, i am knee deep at work atm. Will get to it shortly. Thanks for the nudge
semaj (rep: 10) Apr 8, '25 at 8:39 pm
Hi again Semaj. Just a reminder (the last from me)
John_Ru (rep: 6652) Apr 20, '25 at 2:44 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login