This is my first question in this forum and I am very new to Excel VBA.
I have a text box that has an Exit event. My requirement is that when I fill that textbox and exit from there its values should be populated in ListBox. Now, when I am trying to insert the value again inside textbox, it overwrites previous value inside the ListBox. I want the values to be persistent. So the values entered in text box should come in sequence.
Here is what I tried :
ListBox1.ColumnCount = 2
ListBox1.AddItem
ListBox1.List(0) = TextBox2.Text
But then how can I take the next value from Text box and make it visible in List box...(so if I enter 3 values in text box it should show three rows in Listbox)
When you say ListBox1.List(0) = TextBox2.Text, you are telling the code to always add (replace on 2nd instance) to the first item in the listbox.
Is this what you are trying?
Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 2
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ListBox1.AddItem
ListBox1.List(ListBox1.ListCount - 1, 0) = TextBox1.Text
End Sub
If you do not need a multi column listbox then the below will also do.
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ListBox1.AddItem TextBox1.Text
End Sub
Related
so i am still new to access, but am trying to auto populate a third textbox based on two other textboxes. i have one textbox asking for the DOB (mm/dd/yyyy) and the second text box for their last name. so the third textbox would auto generate a combined value. i know my basic equation would be
=Left([Last Name],3)+Right([DOB],7)
Example: Birthday - 10/01/1978
Last Name - Smith
Result - Smi01/1978
any help would be great
Here is the code you might paste to the code sheet of your user form.
Option Explicit
Private Sub TextBox1_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox1_AfterUpdate()
' runs when the focus leaves TextBox1
SetTbx3 True
End Sub
Private Sub TextBox2_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox2_AfterUpdate()
' runs when the focus leaves TextBox2
SetTbx3 True
End Sub
Private Sub SetTbx3(ByVal Complete As Boolean)
If IsDate(TextBox2.Value) And (Complete = True) Then
TextBox3.Value = UCase(Left(TextBox1.Value, 3)) & Format(CDate(TextBox2.Value), "mm/yyyy")
Else
TextBox3.Value = Left(TextBox1.Value, 3) & Right(TextBox2.Value, 7)
End If
End Sub
There are 3 text boxes, TextBox1 (Name), TextBox2 (DOB) and TextBox3 (File name).
There are two event procedures for each of TextBox1 and TextBox2, Change and AfterUpdate. The Change event occurs when you type a character in the textbox. The Update event occurs when you move the cursor out of the text box after editing. This is for demonstration only. You may not need both.
All of these events call the Sub SetTbx3 which requires one argument, "Complete" which, if True, signifies that the call came from the AfterUpdate event. SetTbx3 will produce a different string for TextBox3 depending upon whether the arguments are complete or not but still provide a string based on the entered string, instead of the entered DOB date, if the date entered isn't recognized as a date.
I have multiple text boxes under each other. I would like them all to be hidden except the first one. If the first box has text in it then the second box should display. If the second box has text in it then the third box will display.
You can use the AfterUpdate event of the textbox, here for the first:
Me!TextBox2.Visible = Not IsNull(Me!TextBox1.Value)
and so on.
Given that your textboxes are named txtBox1 to txtBox6 you can place this procedure in the AfterUpdate event of each of them.
The procedure also sets the value of invisible textboxes to Null. If you don't want that, just comment the regarding line of code.
Private Sub SetTextBoxes(ByVal startWithTextBoxNr As Long)
Const TEXTBOX_COUNT As Long = 6
Const COMMON_NAME As String = "txtBox"
Dim index As Long
For index = startWithTextBoxNr + 1 To TEXTBOX_COUNT
With Me(COMMON_NAME & index)
.Visible = Not IsNull(Me(COMMON_NAME & index - 1).Value)
If Not .Visible Then .Value = Null
End With
Next index
End Sub
The event handlers would look like this:
Private Sub txtBox1_AfterUpdate()
SetTextBoxes 1
End Sub
Private Sub txtBox2_AfterUpdate()
SetTextBoxes 2
End Sub
Private Sub txtBox3_AfterUpdate()
SetTextBoxes 3
End Sub
Private Sub txtBox4_AfterUpdate()
SetTextBoxes 4
End Sub
Private Sub txtBox5_AfterUpdate()
SetTextBoxes 5
End Sub
Additionally you could add SetTextBoxes to the Load event of the form to initialize the controls once for the first textbox:
Private Sub Form_Load()
SetTextBoxes 1
End Sub
The request: Include a check box in my userform that gets checked/unchecked depending on True/False value in column of table
Current state:
I have a user form that is used to add edit or delete entries in a table.
Specifically the editing works in a way that I have a combo box that corresponds with the mail addresses in column A and text boxes for each column of the table.
When you select a mail address in the combo box the text boxes of the user form get filled in with the data from that row.
For the example I would like to add a check box that says "married" and corresponds to a column in the table that has "True" or "False" values. This check box should now be checked/unchecked automatically depending on the value in the corresponding column.
My code:
Private Sub c_01_Change()
' c_01 is the combo box
With c_01
' B_02 is the delete button
' B_03 is the save button
B_02.Visible = .ListIndex > -1
B_03.Visible = True
If .ListIndex = -1 Then Exit Sub
' T_0X are the text boxes
For j = 0 To UBound(.List, 2)
Me("T_" & Format(j, "00")).Text = .Column(j)
Me("T_" & Format(j, "00")).Locked = False
If j > 2 Then Me("T_" & Format(j, "00")).Text = Format(.Column(j), "0.00")
Next
End With
End Sub
Private Sub T_00_Change()
M_text 0
B_02.Visible = T_00.Text <> ""
End Sub
Private Sub T_01_Change()
M_text 1
End Sub
Private Sub T_02_Change()
M_text 2
End Sub
Private Sub T_03_Change()
M_text 3
End Sub
Private Sub T_04_Change()
M_text 4
End Sub
Sub M_text(y)
If c_01.ListIndex > -1 Then c_01.Column(y) = Me("T_" & Format(y, "00")).Text
End Sub
Add a checkbox.
Then check the cell for the value. In my example it is Cells(1,1):
Write this:
CheckBox1.Value = CBool(Cells(1, 1))
I have an Excel user form with multiple multi-line text boxes that need scroll bars. When I click in a textbox to scroll, it starts at the bottom of the text. When this happened with just one textbox on the user form I used this:
Userform1.TextBox1.SelStart = 0
and everything worked. If I try to use that on multiple text boxes in the same form, the scroll bar never appears for any of the boxes. Does anyone know how to fix this?
Update:
Found a quirk that my help narrow down the problem: with multiple textboxes, selstart=0 works on the first box, but then I need a much bigger number for the selstart of the next textbox. Example. The code below puts the scrollbar at the top of both textboxes. The form is shown through a double click on sheet 1 and the the values of the textboxes are create in the initialize sub.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
--------------
Private Sub UserForm_Initialize()
UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
UserForm1.TextBox1.SelStart = 0
UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
UserForm1.TextBox2.SelStart = 200
End Sub
But I could only find that textbox2 had to start at 200 through guess and check. I dont know how to determine where that textbox should start.
I had a breakthrough. If I use SetFocus then do selstart= 0 everything seems to work.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
Private Sub UserForm_Initialize()
UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
UserForm1.TextBox1.SetFocus
UserForm1.TextBox1.SelStart = 0
UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
UserForm1.TextBox2.SetFocus
UserForm1.TextBox2.SelStart = 0
End Sub
I currently have a userform that has a combobox and a listbox. Both contain the same list of items (the combobox has an extra null value). If a user selects an item in the combobox, then the same item will be selected in the list box.
The problem that I am having is coming from the combobox autocompleting when the user tries typing a value instead of choosing one from the combobox. When the user types a value, it will autocomplete what they typed to a value within the combobox. (If I type "8" then the combobox will autocomplete that to "8184123".)
If I set MatchEntry to 2 - fmMatchEntryNone, then the combobox does not autocomplete. However, the combobox does not select a value based on what the user has typed.
Is there any way to stop the combobox from autocompleting while keeping letting MatchEntry stay at 1 - fmMatchEntryComplete? Or is there anyway to implement fmMatchEntryComplete only when the value that the user enters is exactly equal to a value in the list of the combobox?
you can have ComboBox methods and properties work for you
in the form calling sub place:
Sub main()
' code that preceeeds the userform loading...
With UserForm1
'other code to set some userform or userform controls properties...
.ComboBox1.MatchEntry = fmMatchEntryNone ' <--| set this just before showing userform
.Show
End With
Unload UserForm1
' code that follows the userform closing...
End Sub
in the userform code pane place this function:
Function CheckCB(cboBox As MSForms.ComboBox) As Boolean
With cboBox
.Text = .Value '<-- This is the "trick": "refresh" the combobox text
CheckCB = .MatchFound
If Not CheckCB Then
MsgBox "Invalid entry", vbCritical
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Function
despite MSDN online doc the refreshing of .Value actually has MatchEntry and MatchRequired work on it even if MatchEntry is set to fmMatchEntryNone
then you have to call CheckCB() function to prevent leaving userform until your combobox has been entered a valid value
for instance you could place it in any "exit" button click event handler
Private Sub CommandButton1_Click()
If Not CheckCB( ComboBox1 ) Then Exit Sub '<-- if ComboBox check failed then exit
' otherwise let code run ...
End Sub
or, if you wanted the user not to enter any other control until your combobox has a valid value, you must act similarly for every other userform control event handler, i.e. placing If Not CheckCB Then Exit Sub at their beginning
Try using Form1.ComboBox1.AutoWordSelect = True
Custom Autocomplete
Turn off match entry
ComboBox1.MatchEntry = fmMatchEntryNone
When the user types iterate over the combo's list. It there is only 1 possible match, select it.
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Integer, matchIndex As Integer
matchIndex = -1
For i = 0 To ComboBox1.ListCount - 1
If InStr(1, ComboBox1.List(i), ComboBox1.Value) Then
If matchIndex = -1 Then
matchIndex = i
Else
Exit Sub
End If
End If
Next
If matchIndex > -1 Then ComboBox1.ListIndex = matchIndex
End Sub