Tool bar button than can open different UserForms
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Tool bar button than can open different UserForms
Here’s what I have and what I’m trying to achieve. I have a workbook with 20 different sheets; all the sheets are different and are used to generate different types of reports. Each sheet has its own UserForm or (s) that performs a special feature for that selected sheet. What I’m trying to achieve is, by using just one button that whatever sheet is active/visible it would then refer to a selected UserForm for that preferred sheet. I also created a custom toolbar with the use of UI Editor that I was thinking of adding this button too. I’m trying to eliminate buttons on the sheet. I know I can create an additional UserForm to give me a selection to other UserForms but I was hoping for something different….
-
- Administrator
- Posts: 12615
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Tool bar button than can open different UserForms
How do you know which UserForm is associated with each sheet?ABabeNChrist wrote:...Each sheet has its own UserForm...
It would be very easy to create a button that displayed a different userform depending on the Active Worksheet, but you would need some way to associate each worksheet with a specific userform.
StuartR
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Re: Tool bar button than can open different UserForms
If the sheet is named Bedroom then the UserForm is named BedroomHide
All I did was use the sheet name and added the word Hide at the end.
All I did was use the sheet name and added the word Hide at the end.
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: Tool bar button than can open different UserForms
I expect this is the best you can do:
Code: Select all
Select Case LCase(ActiveSheet.Name)
Case "bedrooms"
bedroomshide.Show
Case "kitchens"
KitchensHide.Show
Case Else
'Do nothing, no form available
End Select
-
- 5StarLounger
- Posts: 817
- Joined: 24 Jan 2010, 15:56
Re: Tool bar button than can open different UserForms
Something like:
Code: Select all
Dim frm
Set frm = UserForms.Add(ActiveSheet.Name & "Hide")
frm.Show
Regards,
Rory
Rory
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: Tool bar button than can open different UserForms
userforms.add. Interesting! Add is not shown as a method of the userforms class. But it does work.
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Re: Tool bar button than can open different UserForms
Hi Jan and rory
First off thank you for your assistance
How I came about this was, when I ran the code I would receive no errors, so then I added a MsgBox after Case Else, and then would only receive message no matter what sheet I was on.
Like this “Bathroom(s)
I then realized that using (s) is not a legal recognized name for a UserForm
I thought about renaming all my sheets and removing the (s), but that would entail changing all code that refers to those sheets
Also a couple sheets use 2 words with a space between and of course that will not work with a UserForm name also.
So I thought of possibly using a If Then statement, something like this.
But was unsure how to have a message appear if sheet is not part of selections above
First off thank you for your assistance
Jan I tried using the approach as you described but it seemed to only select Case Else.Jan Karel Pieterse wrote:I expect this is the best you can do:
Code: Select all
Select Case LCase(ActiveSheet.Name) Case "bedrooms" bedroomshide.Show Case "kitchens" KitchensHide.Show Case Else 'Do nothing, no form available End Select
How I came about this was, when I ran the code I would receive no errors, so then I added a MsgBox after Case Else, and then would only receive message no matter what sheet I was on.
Rory your approach did work for most of my sheets except for a couple that have a sheet name followed by (s)rory wrote:Something like:Code: Select all
Dim frm Set frm = UserForms.Add(ActiveSheet.Name & "Hide") frm.Show
Like this “Bathroom(s)
I then realized that using (s) is not a legal recognized name for a UserForm
I thought about renaming all my sheets and removing the (s), but that would entail changing all code that refers to those sheets
Also a couple sheets use 2 words with a space between and of course that will not work with a UserForm name also.
So I thought of possibly using a If Then statement, something like this.
Code: Select all
If ActiveSheet.Name = "Bedroom(s)" Then
BedroomHide.Show
End If
If ActiveSheet.Name = "Bathroom(s)" Then
BathroomHide.Show
End If
If ActiveSheet.Name <> "" Then
MsgBox "Selection not available"
End If
-
- 3StarLounger
- Posts: 392
- Joined: 25 Jan 2010, 12:21
Re: Tool bar button than can open different UserForms
The Case if construction that Jan posts is a streamlined version of the The if.. then construction. The Case Else is when none of the listed sheets is active. With the If.. Then you must create a distinct IF for all the sheets not listed.
Thus the Case If construction is much better than the multiple if ...then.
In the comparison in VB the text strings must match EXACTLY including the case...
Steve
Thus the Case If construction is much better than the multiple if ...then.
In the comparison in VB the text strings must match EXACTLY including the case...
Steve
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Re: Tool bar button than can open different UserForms
Hi Steve
I tried using Jans approach again, making sure the sheet names and UserForms were spelled correctly
Like so
it still wants to End Select
I was also using the tabs accross the bottom to select the correct sheet before running code
I tried using Jans approach again, making sure the sheet names and UserForms were spelled correctly
Like so
Code: Select all
Select Case LCase(ActiveSheet.Name)
Case "Interior"
InteriorHide.Show
Case "Kitchen"
KitchenHide.Show
Case Else
'Do nothing, no form available
End Select
I was also using the tabs accross the bottom to select the correct sheet before running code
-
- 3StarLounger
- Posts: 392
- Joined: 25 Jan 2010, 12:21
Re: Tool bar button than can open different UserForms
Look at your cases:
If the sheet name is "Interior"
The case is looking for "interior" (Lcase of "Interior") and thus will NEVER equal "Interior"
[Similarly "Kitchen" should be "kitchen"].....
The "Case" statements should all have lower case letters since that is what you told it to compare....
Steve
PS if you don't want ot use lowercase you can instead just use:
Select Case ActiveSheet.Name
If the sheet name is "Interior"
The case is looking for "interior" (Lcase of "Interior") and thus will NEVER equal "Interior"
[Similarly "Kitchen" should be "kitchen"].....
The "Case" statements should all have lower case letters since that is what you told it to compare....
Steve
PS if you don't want ot use lowercase you can instead just use:
Select Case ActiveSheet.Name
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Re: Tool bar button than can open different UserForms
Thank you Steve
I see what you mean. I over looked the LCase
I went ahead and selected your suggestion of
Select Case ActiveSheet.Name
Because I’m so familiar with typing with the correct name (Have done it a million times lol) and keeps everything consistent
I see what you mean. I over looked the LCase
I went ahead and selected your suggestion of
Select Case ActiveSheet.Name
Because I’m so familiar with typing with the correct name (Have done it a million times lol) and keeps everything consistent
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: Tool bar button than can open different UserForms
I almost always use either UCase or LCase in select case statements because I have had too many cases where I got the case wrong :-)