How to Display Text on Textbox depending on the Combobox Selection - excel

I am very new to VBA. I have two columns:
Column 1
a
b
c
Column 2
1
2
3
So in if I select a from the combo box - I wanted to text box to show 1.
I have been trying to figure it out through the other posts here but could not get it to work.
If you could explain it to me that would be great!
Private Sub UserForm_Initialize()
With Worksheets("Sheet1")
ComboBox1.List = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value
End With
End Sub
Thank you.

One way is to use VLOOKUP on the combobox value and put this code in the combo box change event so that it runs whenever it is changed. Or you could assign it to a button.
Amend control names as necessary.
Private Sub ComboBox1_Change()
Me.TextBox1.Value = Application.VLookup(Me.ComboBox1.Value, Worksheets("Sheet1").Range("A1").CurrentRegion, 2, 0)
End Sub

Related

Userform and Selecting Cell values

Just playing around with userform. Pretty new to using them . Created a pretty simple userform that find me the percentage change between two values that I enter. (see image below). I'm just interested in going one step further, I want to know how to enter the values based on excel cells that I select. So in this example, if I select K6, the value 15 enters in current year and if I subsequently click on K8, the value 10 enters in base year). Hope that makes sense, let me know if that is possible. Thanks.
current code....
TextBox3_Change()
TextBox3.Value = Format((Val(TextBox1.Value) - Val(TextBox2.Value)) / Val(TextBox2.Value), "#0.00%")
Rather than clicking, why not load the values in those textboxes in the UserForm_Initialize() event? Something like
Private Sub UserForm_Initialize()
TextBox1.Value = Sheets("Sheet1").Range("K6").Value2
End Sub
If you really want to select a cell (after the userform is shown) and then populate the textboxes, then yes that is also possible. Here is an example
For demonstration purpose, I am going to populate 1 textbox. Let's say our userform looks like this. Note, I added a CommandButton1 next to the textbox. I changed the caption to .... We will use Application.InputBox with Type:=8 so that user can select a range.
Next paste this code in the userform code area
Option Explicit
Private Sub CommandButton1_Click()
Dim rng As Range
On Error Resume Next
'~~> Prompt user to select range
Set rng = Application.InputBox(Prompt:="Select the range", Type:=8)
On Error GoTo 0
'~~> Check if the user selected a range
If Not rng Is Nothing Then
'~~> Check if the range is single cell or not
If rng.Columns.Count > 1 Or rng.Rows.Count > 1 Then
MsgBox "Select single cell"
Exit Sub
End If
TextBox1.Text = rng.Value2
End If
End Sub
Demonstration
This isn't a built-in functionality for userforms (unlock the create a graph dialogue box).
One option would be to have the user start by selecting two cells, and then your code runs and calculates the percentage change in those two cells selected and returns the answer in a pop-up. Another option would be to have the userform automatically populate from preselected cells in your workbook (example below).
Private Sub UserForm_Activate()
Dim ActiveR As Long
ActiveR = ActiveCell.Row
TextBox1.Value = Cells(ActiveR, 1).Value
TextBox2.Value = Cells(ActiveR, 2).Value
TextBox3.Value = Cells(ActiveR, 3).Value
TextBox4.Value = Cells(ActiveR, 4).Value
End Sub

Populate textboxes based on Combobox value

I am trying to populate a textbox based on the selection i made on the combobox. It would basically look up the combobox value and return the assigned value from the column (the 3rd) I chose. I have some code done but it returns a type mismatch error
Private Sub UserForm_Initialize()
ComboBox1.List = Sheets(1).Range("C15:C39").Value
End Sub
Private Sub ComboBox1_Change()
TextBox1.Text = Application.VLookup(ComboBox1.Value, Worksheets("sheet1").Range("A15:K39"), 3, False)
End Sub
Can anyone let me know what's wrong and if there is a better way to go about this?
Assuming your combo is not multiselect then load it with all of your range and just show the columns you are interested in. Make the number of columns in the ComboBox the same number of columns in your range.
ComboBox1.List = Sheets(1).Range("A15:K39").Value
ComboBox1.ColumnCount=11
ComboBox.ColumnWidths="0,0,100,0,0,0,0,0,0,0,0"
TextBox1.Value=ComboBox1.List(ComboBox.ListIndex,2) 'the columns are zero-based
Try maybe this:
TextBox1.Text = Application.WorksheetFunction.VLookup(ComboBox1.List(ComboBox1.ListIndex, 0), Range("C15:K39"), 1, False)
Thank you guys,
I ended up going with this
Private Sub ComboBox1_Change()
Dim myRange As Range, f As Range
Set myRange = Worksheets("Sheet2").Range("A2:B26")
Set f = myRange.Find(What:=ComboBox1.Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) '<--| try and find combobox selected value
If f Is Nothing Then
TextBox1.Value = ""
Else '<--| ... otherwise...
TextBox1.Value = f.Offset(, 1)
TextBox2.Value = f.Offset(, 2)
TextBox3.Value = f.Offset(, 3)
End If
End Sub
I had 3 textboxes that i populated with three different columns once an option is chosen from the Combobox.
What i am trying to do is have two last textboxes that I would eventually write numbers in and that would automatically update the corresponding cells.
The Problem I face is updating the cell that relates to the value entered on the combobox.
The code below is very rudimentary and i am wondering if i should do the same manually. First part of the code populates three first textboxes, second part is supposed to populate corresponding cells with value I enter depending on the combobox selection. Please let me know if i am not clear enough.
Private Sub OK_Click()
Sheets("Sheet1").Range("C9") = TextBox4.value
Sheets("Sheet1").Range("D9") = TextBox5.value
End Sub

Userform combobox where i can add or remove items based on cell color coding

I'm trying to code a ComboBox in to a userform, that takes it's items from existing list. The list has color coded cells in the colors of red and green, I would like to add and remove items from the ComboBox based on color coding, red would mean it appears on the list and green means it won't appear. So far i haven't found any sollutions for it.
I would appreciate your help
Say you want to check column C (column = 3):
Dim i as integer
For i = 1 to 50 'row 1 to 50
If Cells(i,3).Interior.Color = RGB(insert RGB of red color) Then 'So if the statement is true, we want to add the cell's contents to the ComboBox.
YourSheetName.YourComboBoxName.AddItem Cells(i,2).Value 'Add cell contents to the ComboBox
End If
Next i
I believe that should do the trick.
Let us assume that we have:
A userform named frmTest
A combobox named cmbTest
Option Explicit
Private Sub UserForm_Initialize()
Dim Lastrow As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
frmTest.cmbTest.Clear
For i = 1 To Lastrow
If .Range("A" & i).Interior.Color = 255 Then
With frmTest.cmbTest
.AddItem (Sheet1.Range("A" & i))
End With
End If
Next i
End With
End Sub

Displays two values (columns) in the mode of selecting an item from the combobox -VBA

Combobox1 lists two columns of the values of cells A and B in Sheet1 Excel. But by choosing one item, only the cell A is displayed (and cell B hidden). How to display both values in the selection mode?
My VBA Code:
Private Sub UserForm_Initialize()
Dim xrg As Range
Set xrg = ThisWorkbook.Worksheets("Sheet1").Range("A1:B5")
With Me.ComboBox1
.List = xrg.Value
.ColumnCount = 2
End With
End Sub
you simply can't
but you can work around it:
place two text boxes each overlapping one "column" in the combobox value display
place the following code in the userform code pane:
Private Sub ComboBox1_Change()
If Me.ComboBox1.ListIndex > -1 Then
Me.TextBox1.Value = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 0)
Me.TextBox2.Value = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1)
End If
End Sub
(change textboxes name to match your choice)

how to change multiple combobox ListFillRange to a named range based on a cell value?

I'm trying to create multiple drop down lists that changes based on which list they want to use.
For example, if they changed the control cell to "List 1", all comboboxes (ActiveX) would change their listfillrange to "=List 1". And if the control cell is changed to "List 2", all comboboxes would update their listfillrange to "=List 2". There's a way to do this with Data Validation but I need the ComboBox function of being able to type and the dropdown would change to show the items in the list beginning with the string that was typed.
I'm a complete beginner with VBA so I do not completely understand nuances compared to languages. I feel that I am missing a lot of proper syntax. I've mashed together some code from searches but none match exactly what I'm looking for so I tried to alter it in some way. I think I butchered a lot of it. I'm so sorry aha
Dim i As Integer
Private Sub ListChange()
If Range("B4").Value = "Bonnie" Then
For i = 1 To 182
' 'Iterate from ComboBox1 to ComboBox182 to change
' listfillrange to 'Bonnie' named range
Set cb = Sheet1.Shapes("ComboBox" & i).OLEFormat.Object.Object
cb.ListFillRange = "=Bonnie"
Next i
ElseIf Range("B4").Value = "Christina" Then
For i = 1 To 182
' Iterate from ComboBox1 to ComboBox182 to change
' listfillrange to 'Christina' named range
Set cb = Sheet1.Shapes("ComboBox" & i).OLEFormat.Object.Object
cb.ListFillRange = "=Christina"
Next i
Else:
For i = 1 To 182
' Iterate from ComboBox1 to ComboBox182 to change
' listfillrange to 'Dianne' named range
Set cb = Sheet1.Shapes("ComboBox" & i).OLEFormat.Object.Object
ComboBox.ListFillRange = "=Dianne"
Next i
End If
End Sub
Also I mentioned that I like using ActiveX ComboBoxes because they show the dropdown dynamically changing as you type the string but I've only been able to make the combobox automatically show the dropdown by having
Private Sub ComboBox1_Change()
Me.ComboBox1.DropDown
End Sub
Private Sub ComboBox2_Change()
Me.ComboBox2.DropDown
End Sub
Is there a way to shorten this or would I have to repeat this for 182 ComboBoxes.
Thank you.

Resources