Populate textboxes based on Combobox value - excel

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

Related

vba- Userform with three comboboxes

I need some guidance how to create a selection for a excel userform that when the user selects CB1 which may be in A2, that it will also select the next cell B2 for the populated field and also select the next cell in C2 for the next populated field. I am not sure if my first selection should be a CB and the next fields maybe a list box or text field.
current code is:
Private Sub Userform_Initialize()
ComboBox_Combox5.List=Sheets("Sheet1").Range("A1:A650").Value
End Sub
Add two text boxes next to your combo-box.
I've left them with the default names of TextBox1 and TextBox2.
Code behind the form is:
Option Explicit
Private Sub Userform_Initialize()
ComboBox_Combox5.List = Sheets("Sheet1").Range("A1:A650").Value
End Sub
Private Sub ComboBox_Combox5_Change()
'Reference to selected value in column A.
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Cells(Me.ComboBox_Combox5.ListIndex + 1, 1)
Me.TextBox1 = rng.Offset(, 1)
Me.TextBox2 = rng.Offset(, 2)
End Sub
Always have Option Explicit at the top of your module.

Updating Value to First Empty cell of Column via Textbox1

I have been using a below piece of code that adds the TextBox1 value into the Col"B" and Col"L" first empty cell of two column 1 is table column and second is range column. Both are from different sheets.
But receiving an error Object does not support this property and range
Any help will be appreciated.
Private Sub CommandButton1_Click()
Sheet37.ListObjects("Table14").ListColumns("Condition").End(xlDown).Offset(1, 0).Value = TextBox1.Value
Sheet5.Range("L2").End(xlDown).Offset(1, 0).Value = TextBox1.Value
ActiveCell.Value = TextBox1.Value
Unload Me
End Sub
When i saw Unload Me, i am assuming that you are using Userform to write data on excel sheet, in your case, the error occur due to you did not mention where the textbox is located:
All the 3 VBA are missing me. that refer to userform that you have created:
Sheet37.ListObjects("Table14").ListColumns("Condition").End(xlDown).Offset(1, 0).Value = me.TextBox1.Value
Sheet5.Range("L2").End(xlDown).Offset(1, 0).Value = me.TextBox1.Value
ActiveCell.Value = me.TextBox1.Value
If you still not clear how to link Userform-textbox-excel, you can refer to my simple instruction on this link Can't update a table with vba
To update the value on ListObject Table, you may use the following method as we need to use databodyrange in order to access the cell in Listobject:
Sub submit()
Dim tb1 As ListObject
Dim s As Long
Set tb1 = Sheet2.ListObjects("Table1")
s = tb1.ListRows.Count
tb1.DataBodyRange.Cells(s + 1, 1) = Me.TextBox1.Value
Unload Me
End Sub

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

How to Display Text on Textbox depending on the Combobox Selection

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

Excel VBA eaching data in different worksheet

I wrote a code that should take value form activecell in sheet1 and look for this value in column A:A in sheet2. When it finds it, code should input data into the cell to the right (still in sheet2). It should input data from txtform (which is data written by user into form textbox.
Private Sub BtnOK_Click()
For Each element In Sheets("sheet2").Range("A:A")
If element.Value = ActiveCell Then
element.Select
ActiveCell.Offset(0, 1).Value = txtform
Exit For
End If
Next
Unload Me
End Sub
It doesn't work. Runtime error 1004. Please, help me.
A number of possible reasons:
Vou are selecting a different cell (element.Select), thereby changing ActiveCell
You are comparing a value (element.value) to an object (ActiveCell); change to ActiveCell.Value
I am assuming txtform is a VBA variable (e.g. a String) declared somewhere above. If not, declare it in a variable or reference the object property.
.
Private Sub BtnOK_Click()
For Each element In Sheets("sheet2").Range("A:A")
If element.Value = ActiveCell.Value Then
element.Offset(0, 1).Value = txtform
Exit For
End If
Next
Unload Me
End Sub

Resources