lets say in a UserForm I have two textboxes, TextBox1 and TextBox2.
How can I dynamically refer to and get the values of these TextBoxes?
Depending on the value of a TextBox, certain other actions should be triggered.
However, I can only make it work, if I explicitly name the TextBoxes and cannot figure out, how to do it dynamically. Can anybody help me?
This works:
If TextBox1.Value > 0 Then
Do something
End If
If TextBox2.Value > 0 Then
Do something
End If
Since there are actually more than 2 TextBoxes, I imagined doing something like this but it does not work:
Dim i as Integer
For i = 1 to 2
If ("TextBox" & i).Value > 0 Then
Do something
End if
Next i
So I guess my question is, how can I combine a string (one part of the name of the TextBox, with a variable (here i) to get another variable (?) which represents the TextBoxes and from which the value can be read? Or is there another/better solution to my problem?
Kind regards
I'm trying to write a for loop that includes the following if statement:
...For x = 1 To lr:
If Range("G" & 1) = "Complete" Then...
The problem is the string "Complete" is an option in a dropdown list from data validation in every cell in column G, and my if statement isn't recognizing it as true even if it's selected. How can I get it to recognize a value from a dropdown? Thanks!
I have had a look online for an answer to this but no joy with coming across one as of yet. Is it possible to have a text box in a UserForm be dynamic?
My current idea is to have 3 columns of text boxes, in Column 1 & 2 the user need to enter there data and was wondering if in Column 3 there is a way to show the value involving Column 1 and Column 2?
If this isn't possible, are you able to reference a cell on a sheet and then get the useform to display the cells value?
Ideally i would like this to be possible as it provides the user with a quick check to ensure they've entered the data right.
Edit 1
TextBox18.Value = (TextBox1.Value + TextBox10.Value) / 2
I found that this line of code allowed me to reference two text boxes dynamically but textbox 18 concatenating rather than added the numbers. Is there a way round this?
Thanks
Alternatively something like this could help, this will add the values and not concatenate them:
TextBox3.Value = (Val(TextBox1.Value) + Val(TextBox2.Value))
Use the TextBox_Change() event method to dynamically update values.
Example:
Sub TextBox46_Change()
TextBox46.Value=(TextBox1.Value + TextBox36.Value) / 2
End Sub
I can click on my ComboBox and see the values of Column1 and Column2, but after I click on off the ComboBox, the value in Column1 is always displayed and I want the value of Column2 displayed.
I tried this:
With ComboBox2
.Value = "None"
.ColumnHeads = True
.ColumnCount = 2
.ColumnWidths = "50;100"
.RowSource = "SetupQuestions!A42:B48"
.BoundColumn = 2
End With
That didn't set the value as I thought it would.
I tried this:
Private Sub ComboBox2_AfterUpdate()
ComboBox2.Value = ComboBox2.Column(2)
End Sub
That didn't set the value as I thought it would.
How can I force the ComboBox to display the value in Column2 after a selection is made?
The DropList of a ComboBox can show multiples columns, but after selecting a row, it can show only one column as Text. To show the second column use the property TexColumn.
Me.ComboBox1.TextColumn = 2
If you are only concerned with appearances, there is a workaround
Private Sub ComboBox2_Click()
With ComboBox2
.Text = .List(.ListIndex, 0) & " | " & .List(.ListIndex, 1)
End With
End Sub
Argh! It's not .Value
It's .Text
Private Sub ComboBox2_AfterUpdate()
Me.ComboBox2.text = Me.ComboBox2.Column(1)
End Sub
I just came across here because I was looking to solve this too. but other people's response helped me find the answer.
If you haven't gotten it yet, here is my solution.
Private Sub ComboBox_AfterUpdate()
ComboboxList.Text = ComboboxList.Column(0) & " " & ComboboxList.Column(1)
End Sub
An alternative you can use without VBA.
Combo row source (adjust for your situation):
SELECT Adults.aID, Trim([Adults].[LastName]) & ", " & Trim([Adults].[FirstName]) AS Expr1
FROM Adults WHERE ((Not (Adults.LastName)=("isNull")))
ORDER BY Adults.LastName, Adults.FirstName;
Basically, make your second column a composite single field via SQL.
Bound column: 1, Column count: 2, Column widths: 0cm;4cm
You can use this technique to display whatever you want by building the string representation as a single field.
The reason is in your setting of ColumnWidth. Your combobox shows two columns. The second one can't be displayed because the total width of your box is insufficient. Therefore you see the first column only. Set the ColumnWidth to "0;100" and you will see the second column. Make sure that there is a working relationship between the width of the box and that of the columns to be displayed within it.
Use a text field ond the right side of the combo box. Set the number of columns in the combo box to 2.
Set the list width the size of both combo and text together (a+b)
Set the columns width for the size of the combo and the size of the text (a;b)
Since the columns property is an 0 based array of columns, set the source of the text field to "=[MyCombo].Columns(1)" to display the second field.
When you drop down, the first column should be exactly under the combo box and the second column under the text box.
When you update the combo, the text box should update its value accordingly.
Additionally you may want to set the text box properties locked to true and enabled to false.
Open Design View
Open Property Sheet
Under the "Data" tab click on the three dots to the right of the "Row Source" item
In the Query Builder that opens, move the desired column you want to view in the combo box field where in the place where the currently viewed column is
Change any VBA code accordingly. For Example: If Column(1) was showing in the combo box field and you moved Column(2) to the place of Column(1), then Column(2) becomes Column(1) and Column(1) becomes Column(2). This will affect any VBA Code referring to Column numbers and also may affect the default value assessed for that Combo Box when a column number is not specified in the VBA code.
Looking for advice on the best way to pass an Id of a value into a parameter in excel VBA.
Essentially I'm trying to replicate getting the value rather than the text itself like for example in html:
<option value="1">Option one</option>
Would return 1. I could concatenate the Id to the start or end of the string with something like:
.additem varList(0, 1) & " | " & varList(1, 1)
But I'm looking for a 'cleaner' option if that makes sense?
Cheers
Create your combobox with at least 2 columns. This can be set using the ColumnCount property, via the VBE or through VBA code.
You can then adjust the ColumnWidths property to make one of the columns a width of 0 so it will not be displayed/visible to users.
WHen you populate the combobox, simply put the ID in one column of the ComboBox, and put the value in the other visible column. The interface will look like this, unless you adjust the columnwidths
Use the BoundColumn of the ComboBox to return the appropriate value, or you can do some iteration over the selected item(s) and refer to the indexed position:
Debug.Print Me.ComboBox.List(0, 0) '# Display the first row item in the first column
Debug.Print Me.ComboBox1.List(0, 1) '# Display the first row item in the SECOND column