Commandbutton page selection - excel

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.

Related

VBA Command button to open Userform returns 1004 error

In a excel project, I have a sheet with data, a userform that draws data from that sheet, and a sheet to paste userform data.
I created a button in the data sheet that should open the user form. For reference the name of my userform is UserForm1. I get a 424 error Object requiredwhen I click on the command button. I would appreciate any help or clarification, seems I am getting my classes/instances mixed up?
Code below:
Private Sub blocksSorter_Click()
Load UserForm
UserForm.Show
End Sub
The form itself is within the same VBA project and is the only form in the project. My UserForm1 code is looks generally like this:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
code
End Sub
Private Sub UserForm_Initialize()
code
End Sub
The form will only show if I do this, and change my UserCode subs to UserForm1 from UserForm.
Private Sub blocksSorter_Click()
Load UserForm1
UserForm1.Show
End Sub
Even though my form name is UserForm1, I had the caption as something else. With the help of braX, I have the following:
Private Sub blocksSorter_Click()
Load UserForm1
UserForm1.Show
End Sub
My form properties are now as follows:
Name: UserForm1
Caption: UserForm1
My UserForm code remains the same.
Start a new VBA project.
Add a form.
Name the form frmOne
Double click on the form and add MsgBox "Hello" to that event.
Add another form.
Name that form frmTwo
Double click on the 2nd form and add frmOne.Show to that event.
Run the second form, and click on it, it should open frmOne. Then click on the frmOne and the msgbox should come up.
Notice that each form has a subroutine called UserForm_Click() but they are different. Does that help clear it up? I'm not sure how else to explain it to you.

How do I make a userform populate as soon as I press the button to open it?

I have a useform that uses a form, but it only populates when I click the 'clear' button.
Private sub userform1_Initialize()
'stuff populating a page
Then on the same page, I have:
Private Sub ClearButton_Click()
Call UserForm1_Initialize
End Sub
How do I go about using:
Call UserForm1_Initialize
somewhere that populates my form when the form pops up?
I tried putting it in the button to open the form (third line):
Private Sub CommandButton1_Click()
UserForm1.Show
UserForm1_Initialize
End Sub
But I get the error:
Compile error:
Sub or Function not defined.
I tried putting my initialize code in the module instead of in the form, but I get the error:
Run-time error '424'
Object Required
I can't put the entire code in the module, because it will not know where to look for cells, right? What am I doing wrong?
I also tried using public instead of private. Same error as above.
Thank you!
Here's an example of my code to activate a userform from my main macro:
UserForm1.Show
Then, the code for the userform itself (right-click UserForm1, then "View Code"):
Private Sub UserForm_Initialize()
ListBox1.List = ProgramScale
End Sub
This populates the listbox on the form automatically when the form appears.
I think Vincent G is right in the comments above in that the sub title for your userform should not have the form number in it, e.g. UserForm_Initialize not UserForm1_Initialize().

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...

Start userform multipage into a specific page

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.

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