I'm trying to populate a listbox in a userform, but the listbox needs to change based on what is in a cell on the active sheet. The complication is that I am trying to refer to a named range, which is on another sheet. So for example, if the cell says "hi" - I would want to check that the cell says hi, and then go to the named range on another sheet called "hi" and bring in the values in that range into the listbox.
Here's what I have so far:
Private Sub UserForm_Initialize()
'Populate Combobox Based on Cell Value
Dim celltxt As String
celltxt = ActiveSheet.Range("cellTest").Text
If InStr(1, celltxt, "hi") Then
'Code to bring in routes from named range called "hi"
ListBox1.RowSource = Worksheets("Sheet4").Range("hi").Value
End If
End Sub
I would repeat the "if-end if" segment of code multiple times based on other cell values, such as "hey" or "what's up."
I keep getting a run-time 1004 error. Help!!
The problem was that I had renamed "Sheet 4" as "DropDown" because it was where I was storing all of my dropdown menus. The code should read:
Private Sub UserForm_Initialize()
'Populate Combobox Based on Cell Value
Dim celltxt As String
celltxt = ActiveSheet.Range("cellTest").Text
If InStr(1, celltxt, "hi") Then
'Code to bring in routes from named range called "hi"
ListBox1.RowSource = Worksheets("DropDown").Range("hi").Value
End If
End Sub
And that works great!
Related
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.
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
I try to make my first UserForm with combo box, I already made this:
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub Reg1_AfterUpdate()
If WorksheetFunction.CountIf(Stock.Range("A:A"), Me.Range.Value) = 0 Then
NsgBox "This is an incorrect item"
Me.Reg1.Value = ""
Exit Sub
End If
With Me
.Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1),
Stock.Range("Lookup"), 2, 0)
End Sub
Private Sub UserForm_Click()
End Sub
The thing is that I don't have any idea how to use combo box with Vlookup function.
I need a combo box because I would like to change a number on Sheet1.
For example:
Harry 10
David 20
A1 Harry B1 10
A2 David B2 20
So I would like to select a name from a Combo Box. After I selected a name I would like to type in a number into a textbox and this number is going to belong for the selected name and sum up with the existing number.
So Harry has 10. After I selected Harry from combo box and set 90 in TextBox the number is going to change for 100 for Harry. That's why I think I have to use Vlookup somehow in VBA.
Thank you
The code below should work as you asked
Sub ChangeValue()
Dim sheetName As String
sheetName = "Name of your sheet"
With ThisWorkbook.Sheets(sheetName)
'For each name in your range
For Each cell In .Range("Your range where names are")
'If the name is the one selected in your combobox
If (cell = YourComboBox.Text) Then
'Increment value
.Range(cell.Address).Offset(0, 1).Value = _
.Range(cell.Address).Offset(0, 1).Value + YourTextBoxValue.Text
End If
Next cell
End With
End Sub
Usage
Replace Name of your sheet with the name of the sheet where the names are.
Replace Your range where names are with the range where we can find all the names in your sheet.
Replace YourComboBox and YourTextBoxValue with the names of your components in your userForm.
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
I am creating userform which I will be using to insert data and then make some other stuff.
I have a Userform with ComboBox and few TextBoxes. ComboBox is filled with data from range. I want to change values of TextBoxes depending on ComboBox value. Values of TextBoxes should be filled with specific values from worksheet. I thought about creating For Each loop to determine Row of chosen ComboBox value and then change TextBoxes using Row number and setting proper offset.
Worksheet is table with headers and filled with data such as name, city etc.
However my code does not work within Userform.
Any ideas what is wrong or maybe a different approach to a problem?
klient = ComboBox name
Private Sub klient_Change()
Dim MyCell As Range, MyRange As Range
Dim wiersz As Long
Set MyRange = Range("klienci")
For Each MyCell In MyRange
If klient.Value = MyCell.Value Then
wiersz = MyCell.Value
Exit For:
End If
Next
MsgBox (wiersz)
End Sub
As follow up from comments to the questions, this code works:
Private Sub klient_Change()
MsgBox Range("klienci").Cells(1 + Klient.ListIndex, 1).Address
'do something else, e.g. get element one to the right:
MsgBox Range("klienci").Cells(1 + Klient.ListIndex, 1).Offset(,1).Address
End Sub