Start userform multipage into a specific page - excel

I have a Userform named SheetBox
SheetBox contains a 3-page multipage window
"page1" for selecting sheets to import "page2" contains a pseudo
progress bar "page3" for selecting sheets to protect
What I need now is a method to open a specific page upon opening the Userform when a button on a worksheet is clicked
ex:
ImportBttn opens page1 of userform
ProtctBttn opens page3 of userform
I'm doing this to reduce the number of userform I needed to create, instead of creating 3 separate userforms. This also helps reduce the filesize.

This works too
Sub ImportBttn_Click()
Load SheetBox: SheetBox.MultiPage1.Value = 0: SheetBox.Show
End Sub
Sub ProtctBttn_Click()
Load SheetBox: SheetBox.MultiPage1.Value = 2: SheetBox.Show
End Sub
this loads sheetbox first, changes the multipage page and shows it afterwards
but thanks for the caller method, will be useful when I need to know what button gets pressed

In the UserForms Initialise Event, use Application.Caller to detect which button on the worksheet was pressed, and then set the multipage
Private Sub UserForm_Initialize()
Select Case Application.Caller
Case "ImportBttn"
`1st tab
Me.MultiPage1.Value = 0
Case "ProtctBttn"
`3rd tab
Me.MultiPage1.Value = 2
End Select
End Sub

My project using several multiPages. Since the user-defined names for the tabs are dynamic, I set the default Tab with ...
Sub iconDailyEntry_Click()
Call init_GUI(frmGUI.mulGUI, "pgDaily")
End Sub
Sub init_GUI(mPage As MSForms.MultiPage, pgName As String)
mPage.Value = mPage(pgName).Index
mPage.Parent.Show
End Function
Rinse & repeat for each button's click event. Cheers.

Related

How to access a VBA Userform Button control code programmatically

I want whenever I click on my ActiveX command button on Sheet1 it open VBA Editor and jump directly to the code I wrote for a button on my userform. I know the way to jump directly to code for a module but I couldn't find a property for userform button control (something like ThisWorkbook.VBProject.VBComponents("UserForm1").Controls("MyButton"))
Following is my code for jumping right into my Madule1 code:
Private Sub CommandButton1_Click()
Dim Wb As Workbook: Set Wb = ThisWorkbook
'---Open Module1
Application.VBE.MainWindow.Visible = True
Wb.VBProject.VBComponents("UserForm1").Activate
End Sub
I thaught these pictures would help to better understand my goal:
1-I have an ActiveX command button on sheet1
2-I have userform with a commandbutton on it
3-This commandbuttun has it's own code which I want to jump to it's code
If you want to jump to that specific sub directly, try this:
Private Sub CommandButton1_Click()
Application.VBE.ActiveVBProject.VBComponents("UserForm1").CodeModule.CodePane.Show
With Application.VBE.ActiveCodePane.CodeModule
lStartLine = .ProcStartLine("CommandButton1_Click", 0) '"CommandButton1_Click" being the name of the sub you want.
.CodePane.SetSelection lStartLine, 1, lStartLine, 1
End With
End Sub
First line should show you the code panel for the userform, and the second part selects the row down by the sub you are looking for by name.

Excel VBA Userform read current values when opening

I am trying to make an user form for hiding/showing different columns of the worksheet.
So i've made a button which opens the userform (called 'hider') with the following code:
Private Sub CommandButton1_Click()
Hider.Show
End Sub
The userform than currently contains two checkboxes which hide the selected columns using:
Private Sub Week3_Click()
Range("N:Q").Columns.Hidden = Not Week3
End Sub
So if the checkbox is 'checked' the columns are shown and if 'unchecked' the columns are hidden, this part works, except every time the userform opens it will reset the checkboxes to their native state of 'unchecked' while the columns remain hidden (which is ok).
So my question is:
How can I sync the checkboxes in the userform to the currently active value of the columns? I was thinking about making a sync button on the userform or an action to read all the current values when opening the userform, but i couldn't get that to work.
If I understand your question correctly ("...or an action to read all the current values when opening the userform") just use the following code within the Userform code module to synchronize your checkbox when opening:
Private Sub UserForm_Initialize()
'Me.Week3.Value = IIf(ActiveSheet.Range("N:Q").Columns.Hidden = True, False, True)
Me.Week3.Value = Not ActiveSheet.Range("N:Q").Columns.Hidden ' simplified due to comment thx Mathieu Guindon
' ...
End Sub

Commandbutton page selection

So I have a commandbutton on a excel sheet and when I click on it brings up the userform But the page isn't starting from "Page1" rather its the page I was last using in the userform developer window. Therefore is there a way once clicked into the commandbutton the userform load up the first page everytime?
At the moment the commandbutton contains this (Userform is named as "WizardProp":
Private Sub CommandButton1_Click()
WizardProp.Show
End Sub
and the Openform contains this:
Sub openform()
WizardProp.Show
WizardProp.tbClient.SetFocus
End Sub
The visible page number of a MultiPage object is defined by its Value (starting with 0).
Is "tbClient" the MultiPage object on your userform? Then place this code in your userform's initialization:
Private Sub WizardProp_Initialize()
WizardProp.tbClient.Value = 0
End Sub
Would Unload Me solve your problem? insert on the close of the userform. The Only issue is that it would rest the userform, data and all.

Why, after launching a userform from a userform, does unloading the 2nd userform close both?

A button on a worksheet launches a macro that opens a userform (say, userform1). Userform1 is loaded non-modal in order for the user to use both userform1 and the worksheet (i.e., click cells) for input. There is a button on userform1 that, when clicked, opens another userform (say, userform2). Userform2 is modal. Clicking a Cancel button on userform2 unloads userform2 as it is supposed to; however, it, for some reason, also unloads userform1, which I do not want. If I make userform1 modal, then unloading userform2 does not unload userform1; however, the user can no longer use (i.e., click) the cells in the worksheet. I cannot find any info that will give me a clue as to why unloading one userform unloads both.
I am very happy I just stumbled upon this old thread.
What I discoverd is that the problem goes away when the VBA-editor window is closed. You really have to CLOSE it, minimizing the window is not enough. It also does not matter if the window is opend on the same screen or not. Only closing it did the trick for me. What I discoverd is that as soon as Form2 was unloaded, the VBA-editor shows Form2, no matter what other code-module I was just in.
I aimed to isolate the problem in a TestWorkbook.xlsm(review code below). I tried out the suggestions DoEvents and a line of code after Form2.Show and both helped. In my DevelopmentAddIn.xlam they did not help, the 1st form still closes. So the problem could still lay in the more complex code of my AddIn.
But like I said, closing the VBA-editor window does the trick, all though I still do not understand why.
TestWorkbook.xslm (two userforms Form1 and Form2 and a code module mLoad)
mLoad:
Option Explicit
Public Changed As Integer
'***Load the 1st form
Public Sub LoadFirstForm()
Load Form1
'allow user to change the active workbook
Form1.Show vbModeless
End Sub
'***Load a 2nd form (from Form1)
Public Sub LoadSecondForm()
Dim a As Integer
Load Form2
'continue after 2nd form closes
Form2.Show vbModal
'suggestions
a = 1
DoEvents
'2nd form was changed
If Changed = 1 Then
Form1.InfoBox.Value = "Changed"
'process changes
'2nd form is unchanged
Else
Form1.InfoBox.Value = "Unchanged"
End If
End Sub
Form1 (with button cmdLoad and textbox InfoBox)
'***Load the 2nd form (from Form1)
Private Sub cmdLoad_Click()
LoadSecondForm
End Sub
Form2: (with button cmdOK)
Option Explicit
'***Initial status is 'unchanged'
Private Sub UserForm_Initialize()
Changed = 0
End Sub
'***Status is 'changed'
Private Sub cmdOk_Click()
Changed = 1
'close 2nd form and continue
Unload Me
End Sub
UserForm2.Show
a = 1 ' only 1 more line of any code to execute
this will do the trick. At least this worked for me with the same issue...

Initializing a userform class

I have a userform created and several buttons (the number will be set during runtime) in excel. When clicked, each button will open its own version of the userform (same userform but each button will have its own userform, for example: button1 will open userform1, button2 will open userform2, etc. with each userform being the same userform class).
I am thinking of somehow creating a userform class and each button will instantiate a userform object.
I'm just not sure what the proper code/syntax is to achieve this.
I think this may help, based on your last comment.
Just create one user form. Then create a function that opens the userform and fills the specific data with parameterized variables. Then for each button call the pass the variable into the function. Something like this:
Private Sub Button1_Click()
load_user_form "Handy", "Code"
End Sub
Private Sub Button2_Click()
load_user_form "different", "data"
End Sub
Function load_user_form(strField1 as String, strField2 as String)
userForm.Show
userForm.TextBox1.Value = strField1
userForm.textBox2.value = strfield2
End Function
This code may not be spot on, but it will get you there.

Resources