
Originally Posted by
Greg M
Hi All4Excel,
I hope the attached workbook helps to get things started.
The workbook contains a sheet called "Search" which is the heart of the action. This sheet contains a named range called "SheetNames" which holds the names of each of the sheets in the workbook & is used to populate the dropdown list for the combobox. This range is updated automatically (using VBA code) whenever a worksheet is added or deleted.
The combobox can be moved to wherever you wish on the "Search" sheet.
The first row of the "SheetNames" range (and therefore the dropdown list) will always contain a blank as you requested.
At present the dropdown list does NOT include the "Search" worksheet itself - I can include this very easily if you wish.
The workbook contains two VBA code modules, named "ThisWorkbook" and "Module1". This code might not be the easiest for a beginner to understand, but I'll try to give you the general method of operation.
The main code is the "UpdateList" routine. This first clears the existing list of worksheet names and deletes the existing "SheetNames"named range. The code then takes the names of each of the worksheets (apart from the "Search" worksheet) & enters them in column A of the "Search" worksheet, beginning at cell A2. Having done this, the code updates the "SheetNames" range to reflect the number of worksheets the workbook now contains.
Now all we need to do is to trigger the "UpdateList" routine. Whenever a new worksheet is added, that sheet is Activated (selected). Similarly, whenever a worksheet is deleted, some other worksheet in the workbook is Activated. Excel itself calls a routine called "Workbook_SheetActivate" whenever a worksheet is Activated, so we can use that routine to call our "UpdateList" routine.
To avoid calling the "UpdateList" routine when we're simply navigating around the worksheets we use a simple test in the "Workbook_SheetActivate" routine. The variable "intTotalSheets" contains the number of sheets in the workbook & is updated (by our code) when the workbook is opened and also by the "UpdateList" routine. Whenever the "Workbook_SheetActivate" routine is called we check the number of sheets the workbook contains & if that is NOT equal to the value contained in "intTotalSheets" it means that a worksheet has been added or deleted & so we call the "UpdateList" routine in such a case.
I hope this gives you some understanding of what's going on, but please feel free to ask me for further information.
Best regards & happy coding,
Greg M
Bookmarks