VBA Excel Exiting after update event fires enter event - excel

Let's say I had a combobox1, 2, 3 and a textbox1 on a userform. After update event was associated to combobox1. When Tab key was pressed after update event was fired (filling comboboxes 2 and 3). In the code of this event, just before exit sub there was textbox1.setfocus to skip entering other coboboxes in the tab order (combobox 1,2,3, textbox1). It worked just fine.
When I added another combobox that is now combobox2, the tab order was changed to 1,2,3,4.
After update is still associated to combobox1 and textbox1.setfocus is last line before exit sub. Unfortunately when exit sub line for after update is executed, it fires combobox3 enter event and moves focus to combobox3. It's more incomprehensible because tab order is combobox1,2,3,4 so it skips the tab order as well.
When I debuged code, the focus to textbox1 was set as it should but still just when exit sub is executed, code line is moved to combobox3 enter event...
Any tips appreciated.

in your userform code pane act as follows:
a userform scoped Boolean variable
hence, place this before any Sub/Function code
Dim SetTextBox1Focus As Boolean
in your ComboBox1_AfterUpdate() event handler place:
Private Sub ComboBox1_AfterUpdate()
If (condition that checks if the data entered in combobox1 exists in the database) is True Then
...
your code to fill comboboxes 2, 3 ad 4
...
Me.TextBox1.SetFocus
SetTextBox1Focus = True '<--| "flag" TextBox1 to receive the focus
End If
End Sub
add an Enter event handler for all userform controls whose tab index is in between ComboBox1 and TexBox1
for instance, assuming those are ComboBox2, ComboBox3 and ComboBox4
Private Sub ComboBox2_Enter()
CheckSetTextBox1Focus
End Sub
Private Sub ComboBox3_Enter()
CheckSetTextBox1Focus
End Sub
Private Sub ComboBox4_Enter()
CheckSetTextBox1Focus
End Sub
add the following CheckSetTextBox1Focus() Sub
Sub CheckSetTextBox1Focus()
If SetTextBox1Focus Then
Me.TextBox1.SetFocus
SetTextBox1Focus = False
End If
End Sub

Related

Auto Select Checkbox triggered by another checkbox

I am trying to auto select a checkbox if any another checkbox is selected. All this checkboxes are on the same sheet and basically i want check box 7 to tick if checkbox 3,4 or 5 is selected.
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Start Page").CheckBox3 = True Or Sheets("Start Page").CheckBox4 = True Or
Sheets("Start Page").CheckBox5 = True Then
Sheets("Start Page").CheckBox7 = True
Else
End If
End Sub
Any help would be greatly appreciated.
Even if your checkboxes are linked to a cell (LinkedCell-property), the Worksheet-Change-event is not triggered when you click on a Checkbox.
You need to catch the Click-Event of the checkboxes. For every checkbox, put a Click-event routine into the sheet module. To prevent that the logic if or if not to set the "calculated" checkbox is repeated, let those event handler call a common routine that does the calculation.
Private Sub CheckBox3_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox4_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox5_Click()
Call SetMyCheckBox
End Sub
Sub SetMyCheckBox()
Me.CheckBox7.Value = Me.CheckBox3.Value Or Me.CheckBox4.Value Or Me.CheckBox5.Value
End Sub
You should, by the way, consider to give your checkboxes more meaningful names.

Excel VBA UserForm TextBox Does Not Recognise Exit

Hopefully this is a simple one for somebody out there. I have an Excel VBA UserForm with a number of text boxes for the entry of parameters. In addition, there is an image on the userform. As the user clicks into a textbox, the image on the UserForm is changed by triggering the _Enter() event to show an image of the parameter in question. This works fine and is not a problem. However, as the user exits the textbox, I want the image to revert back to the original image. I've tried the Events for _Exit() and _AfterUpdate() to make this work and although it works fine if I update the parameter, it doesn't work if I don't update the parameter. In essence, the image changes as I enter the text box but doesn't change as I leave unless I update the value in the text box.
Does anybody have any ideas why this is so and what I might be able to do about it?
Update (with code as requested). I've now figured out that the textbox Exit event is not being triggered because I have more than one frame on my UserForm and I am clicking on a control in a different frame. I've made the following example.
and the following code;
Option Explicit
Dim TextBox1Data As Variant, TextBox2Data As Variant
Private Sub TextBox1_AfterUpdate()
TextBox1Data = TextBox1.Value
Debug.Print "AfterUpdate Textbox 1"
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit Textbox 1"
End Sub
Private Sub TextBox2_AfterUpdate()
TextBox2Data = TextBox2.Value
Debug.Print "AfterUpdate Textbox 2"
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit Textbox 2"
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Array("Item 1", "Item 2", "Item 3", "Item 4")
ComboBox1.Text = ComboBox1.List(1)
End Sub
If I click into TextBox1 and then click into either ComboBox1 or CheckBox1, the Exit event for TextBox1 is triggered. However, if I click into TextBox1 and then click into either CheckBox2 or CheckBox3, the event is not triggered. I assume that this is because the CheckBoxes are in a different frame. Is this the case? Is there a way of triggering an Exit event for a TextBox when moving from one frame to another?
It seems like this is a known problem - see Microsoft Q210734. Rather than use the Set Focus method suggested by MS, I've now added an Event for exiting the Frame where the textboxes are located, e.g. using my example code, I added the following;
Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exiting Frame"
End Sub
What happens now is that when moving from one control to another within Frame1, the normal Exit event for the control works as expected and when moving from Frame1 to Frame2, the Exit event for the frame works.
Hope this helps someone in the same predicament.

in what event the submit button enable should be written if text boxes are filled, for VBA

my question is in what event the submit button enable should be written if text boxes are filled, for VBA
for example im talking about the Private sub userform initialize()
i had created a userform where i used 2 frames. while the macro runs it initialize the first frame and user logins using that form to go the next form which is in the other frame.
in this second frame form i have 3 text box fields. only if the user inputs all the three text box then the command button should be enabled. im now stuck with in which event this code should be written.
Thank you
assuming the button is named after "CommandButton1" and the three textboxes are name after "TextBox1", "TextBox1" and "TextBox3", then in the Userform code pane add the following:
In UserForm_Initialize place:
Me.CommandButton1.Enabled = False
add Change event handler for all those three textboxes
Private Sub TextBox1_Change()
checkEnableButton
End Sub
Private Sub TextBox2_Change()
checkEnableButton
End Sub
Private Sub TextBox3_Change()
checkEnableButton
End Sub
finally add the following:
Sub checkEnableButton()
Me.CommandButton1.Enabled = Me.TextBox1.Value <> "" And Me.TextBox2.Value <> "" And Me.TextBox3.Value <> ""
End Sub

Set focus on UserForm Textbox on tabbing from another Textbox

I have a Userform in Excel. It has 6 Textboxes, they are the only controls on the Userform with TabStop set to True.
I can tab and move through the textboxes. I cannot get SetFocus to work on Events fired when moving between the Textboxes. I can put a CommandButton on the userform with Userform16.Textbox1.Setfocus and it works as expected to move the focus to Textbox1.
I set up a simple test event (see below) to move the textbox focus back up to TextBox1 when Textbox2 is entered. It moves focus to Textbox3 when I tab out of TextBox1.
Private Sub TextBox2_Enter()
Cancel = True
UserForm16.TextBox1.SetFocus
End Sub
By putting a Stop in the above, I can see that the event is firing as expected, but it will not allow me to control the focus the next control.
I get the same results with or without the Cancel = True statement in the sub.
I set up a simple test event (see below) to move the textbox focus back up to TextBox1 when Textbox2 is entered, it actually moves focus to Textbox3 when I tab out of TextBox1.
You can't set focus to another control in the _Enter() event. If you try to then the code will move focus to control which has the next TabIndex
For example
Let's say you have 5 textboxes with the following TabIndex
TextBox1 (TabIndex 0)
TextBox2 (TabIndex 1)
TextBox3 (TabIndex 3)
TextBox4 (TabIndex 4)
TextBox5 (TabIndex 2)
Now if you have this code
Private Sub TextBox2_Enter()
TextBox3.SetFocus
End Sub
The moment you press TAB from TextBox1, the focus will move to TextBox5 (and not TextBox3) as it has the next TabIndex.
Also Cancel = True will not have any effect because it is not an argument of _Enter() like it is of say Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Edit
BTW, the only time the focus will come back to Textbox1 in your scenario is when there are only two TextBoxes in the form.
I know this is old but this answered my question and I can't comment. However, Stan in reference to your comment on June 8, 2017 I believe you were looking for something like the code below which will highlight the text in the text box when you use it with Cancel = True. I use it in the textbox exit event. This will be the indication to the user that the text box is selected.
With Me.TextBox1
.Value = "Full Name"
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Cancel = True
While it is true that _Enter() event can not handle .SetFocus, the _KeyDown() event can! And thats a pretty good workaround, you just need to monitor if the TAB key was pressed.
So the code would look like something similar where TextBox1 is the one you leave and TB2 is the one you enter;
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Then TextBox1.SetFocus 'where 9 is the KeyCode for the TAB button
End Sub

Userform showing when not wanted, not called for

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.

Resources