In the vbEditor, in the project pane (Ctrl+R if it isn't showing), right click on ThisWorkbook select View Code (or just DoubleClick) and Paste all of the following code.
1/. Workbook_NewSheet
This is activated when a new sheet is added.
It clears the ListBox then loops through the sheets in the workbook and adds all the names, including the new one, back into it.
2/. Workbook_SheetActivate
This is activated when a sheet is selected
All this does is check that the sheet is not a chart sheet (a chart sheet has no cells).
If it isn't it selects Range("A2").
This can be any cell except, in this case, "A1" where we want to trigger the Listbox, the reason for this is purely to select any cell other than A1.
It also means that you don't need a button on every sheet, all you need to do is copy the sheet when you need a new one, or format cell A1 on any sheet you import. You'll soon workout how to change the location of this activating cell.
3/. Workbook_SheetSelectionChange
This is activated when a new cell is selected
It uses the Intersect function, possibly the most useful function you can use for triggering code on this event.
Highlight Intersect and press f1 to see how it works (Excel Help)
In this case it is not really needed but it is a good method to know about.
All that is done here is that if A1 is selected then the userform shows'
The code could simply have been
4/.The UserForm with one ListBox is very basic.
All it does is read all the sheet names when it is loaded and activate the chosen sheet when clicked.
Because it is unloaded every time it is used, it subsequently is automatically updated when a sheet is added deleted or renamed.
To use this in other existing sheets
a/. Highlight it in the project window Then File > Export File.... Select your directory & save, (call it something identifiable)
b/. To add to someother file. Then in the VB Editor File > Import File....Find the file ( whatever.frm) > Open
That's it! Simples! .........
However you could in future simply use the Demo File as a template for a new file.
Cheers
If you need any further information, please feel free to ask further
However
If this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody!
Also
If you are satisfied by any members response to your problem please consider using the scales icon top right of thier post to show your appreciation.
Bookmarks