Having a weird issue with Excel Userforms.
I've got 2 userforms, one with a textbox and the other with a button. The one with the textbox uses Textbox_Change event to track what's in the box and after 10 characters have been entered it loads Userform2.
When Userform2 initialises Userform1 is unloaded. Userform2 has a button which then reloads Userform1.
This is a very slimmed down version of my end result, but I needed to see it in it's most basic form to make sure it wasn't my code that was causing an issue.
Anyway, when I'm typing in Userform1's text box it tracks the changes of TextBox1, but after it's reloaded it stops tracking.
Below is my code.
Userform1:
Dim Iteration As Integer
Private Sub TextBox1_Change()
Debug.Print Me.TextBox1.Value
Iteration = Iteration + 1
If Iteration = 10 Then
UserForm2.Show
End If
End Sub
Private Sub UserForm_Initialize()
Iteration = 0
End Sub
Userform2:
Private Sub CommandButton1_Click()
UserForm1.Show
End Sub
Private Sub UserForm_Initialize()
Unload UserForm1
End Sub
It's as simple as it could possibly be.
When I first loaded Userform1 I entered into the text box "1234567890", which outputted the console:
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
As expected.
However after I've loaded Userform2 and then click the button to reload Userform1, when I enter "abcdefghij" into the textbox it doesn't output anything to the console.
Is there some fundamental concept of userforms that I'm missing here? Or something I'm getting wrong? I can't see anything that would be causing this.
I've been reading up on what unloading and show actually does and I can't find anything that'd affect whether or not the event should fire. From what I know when you run Userform.show it reinitialises everything, but even if it didn't it should still output SOMETHING to the console.
Can anyone help?
EDIT:
even if I change Userform 1 to be:
Dim Iteration As Integer
Private Sub TextBox1_Change()
Debug.Print Me.TextBox1.Value
Iteration = Iteration + 1
If Iteration = 10 Then
Unload Me
UserForm1.Show
End If
End Sub
Private Sub UserForm_Initialize()
Iteration = 0
End Sub
It still doesn't track changes on the second time round.
Okay, I fixed the issue.
I'm not sure why it works, need to look into it further, but if I add vbModeless to the .show function when initialising a userform it allows me to track changes on the second form.
Related
I have 2 Userforms, Userform1 and Userform2. I would to like to code Userform2 so that when the user closes it using the X button on the top right corner, it closes and Userform1 is automatically opened. The user can go from Userform1 to Userform2 with a CommandButton that has the following code:
Private Sub CommandButton1_Click()
Unload Me
UserForm2.Show
End Sub
After closing Userform2, I managed to automatically open Userform1 with the following code in it:
Private Sub UserForm_Terminate()
UserForm1.Show
End Sub
The problem is that this only works once. The second time the user clicks CommandButton1, Userform1 is closed and Userform2 showed, but Excel hangs up. The only way to fix it is to stop all the code from the developer environment or force quit Excel.
Using the UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) event isn't possible because that way, Userform2 never reaches the Unload part and is just kept in the background while Userform1 launches again, like this:
There is no need to unload UserForm1. Simply hide it. Also there is no need to show UserForm1 from UserForm2. It will automatically get shown when UserForm2 unloads. Is this what you are trying?
In a module
Option Explicit
Sub Sample()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
In Userform1
Option Explicit
Private Sub CommandButton1_Click()
Me.Hide '<~~ Hide the form
Dim frmTwo As New UserForm2
'~~> Show the form in Modal(default)
frmTwo.Show
'~~> Show UserForm1 when UserForm2 unloads
Me.Show
End Sub
EDIT
Thank you for taking the time to help me! Unfortunately this is not what I am looking for. I'm specifically trying to unload Userform1 when showing Userform2 and viceversa because I don't want/need to preserve any data. I know manually clearing the form would also work, but the other thing is that this is only an example. My real code has many forms and I don't want all of them to remain loaded in the memory. That could potentially cause some performance issues. – Fernanda 10 mins ago
This doesnt crash for me
In a module
Option Explicit
Sub Sample()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
In Userform1
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
Dim frmTwo As New UserForm2
frmTwo.Show
End Sub
In Userform2
Option Explicit
Private Sub UserForm_Terminate()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
Excel 2013
I have read several questions that ask this same question and none of those answers seem to be working for me.
I have a userform where a combobox is used to select the number of variables (1, 2, or 3) to be used later in the module. Then an OK button.
I have tried declaring a global variable and making it public in the first line of the module and it seems to work when when click through the macro manually, but when I try to use it as a user would it drops the variable for whatever reason?
Module1
Public variable As Integer
Private Sub Button1_Click()
UserForm1.Show
End Sub
I assume the Button1_Click() has nothing to do with anything, but wanted to show it and confirm it wasn't part of the problem
Userform1
Private Sub UserForm_Activate()
ComboBox1.AddItem 1
ComboBox1.AddItem 2
ComboBox1.AddItem 3
End Sub
user selects 1 2 or 3
Userform1 still
Private Sub OK_Click()
If ComboBox1.ListIndex = -1 Then
MsgBox "Select a number"
Else
variable = Me.ComboBox1.Value
Range("D1").Value = variable'testing
Unload Me
End If
Range("D2").Value = variable 'testing after "Unload Me"???
End Sub
'Module1 again
Sub Macro2()
Range("D3").Select
ActiveCell.FormulaR1C1 = variable
End Sub
When I F8 through UserForm_Activate() --> OK_Click() --> Macro2() it remembers my variable and puts it in cell D3.
If I just click the buttons as a user would and then manually kick off Macro2() it thinks my variable is 0 (not an option) and I get 0 in cell D3, rather than the variable that was selected by the user.
Thanks for your help!
im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.
I wonder if there is a way to speed up time of clicking CommandButtons?
Thing is when I use my UserForm in many cases I click "next" button multiple times, however button does not react as fast as I click. So there are times I clicked 3 times and "next" button took only to second record rather then forth.
Edit:
Private Sub CommandButton1_Click()
TextBox1 = TextBox1 + 1
End Sub
Private Sub CommandButton2_Click()
TextBox1 = TextBox1 - 1
End Sub
Private Sub UserForm_Initialize()
TextBox1.Value = 1
End Sub
The anwser credits goes to Rory who pointed out DblClick Event. So having that in mind it is easy to to change code to work better. All we need to do is add Dblclick Event and work it as minus / plus 2 so it looks like working faster then before.
In my case I needed to add some restriction with numbers below zero but main idea is wrote below :)
Private Sub CommandButton1_DblClick()
TextBox1 = TextBox1 + 2
End Sub
Private Sub CommandButton2_DblClick()
TextBox1 = TextBox1 - 2
End Sub
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...