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
Related
I have been struggling with VBA code that selects entire row and column of active cell. I tried many codes, but all of them failed. I don't want to make any changes to range, I need to just select it.
How selection after macro should look like:
Selected column and row
Last version of code I came up with, still not working. Any ideas how to achieve it or fix it?
Private Sub CommandButton7_Click()
Dim Column As range
Set Column = ActiveCell.EntireColumn
Dim Row As range
Set Row = ActiveCell.EntireRow
Dim rng As range
With ActiveSheet
Set rng = Union(.range(Column), .range(Row))
End With
rng.Select
Unload Me
End Sub
You were close, but this can done simply with:
With ActiveCell
Union(.EntireRow, .EntireColumn).Select
End With
Or just a one-liner:
Union(ActiveCell.EntireRow, ActiveCell.EntireColumn).Select
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 have 3 comboboxes namely combobox1, combobox2, and combobox3 with the same items each.
The items are, “aa”, “bb”, “cc”, and “dd”.
ALSO, these comboboxes are in the userform.
So, i have 3 comboboxes in USERFORM,
if I select “aa” in combobox1, how can I make “aa” not appear in the other 2 comboboxes, so that it
won’t be reselected again.
It’s like, if I select “aa” in combobox1, and “bb” in combobox2, the remaining items that is available in
combobox3 must be “cc” and “dd”.
But I don’t know how to do it. Can you help me with the codes? Thank you a lot.
Ps. I am using Microsoft VBA in excel
Pps. I am not pro.
Here is a basic sample of how you can achieve what you are looking for. You will need to change the code to work in your own project.
This is assuming the user will make a selection from the ComboBoxes in a specific order every time. E.g. this will work for selecting a value from ComboBox1 first and then provides the list for ComboBox2 but won't work (in its current form) for removing ComboBox2's selection from ComboBox1 list. It can be modified to do so, so feel free to do that if need be.
You can write your list to a worksheet anywhere you want - generally it's useful to do so in a hidden column or hidden helper sheet. This example has the values "AA" to "GG" in cells P6 to P12 on Sheet2 of the workbook.
The code loops through the range of your to to add items to the ComboBox 1 by 1, if the value is = the previous ComboBox selection then skip it otherwise add the value to the ComboBox.
Private Sub UserForm_Initialize()
'This Sub populates the first combobox with all values in the list
Dim TargetCell As Range
Dim ListRange As Range
With ThisWorkbook.Sheets("Sheet2")
Set ListRange = .Range("P6:P12")
For Each TargetCell In ListRange
Me.ComboBox1.AddItem TargetCell.Value
Next TargetCell
End With
End Sub
'__________________________________________________________________________________
Private Sub ComboBox1_Change()
'This Sub is similar to above, but has an If statement to check if the value on the sheet_
'is the same as the selected value in the previous combobox1. If it is it's ignored_
'otherwise Combobox2 has the value added.
Dim TargetCell As Range
Dim ListRange As Range
With ThisWorkbook.Sheets("Sheet2")
Set ListRange = .Range("P6:P12")
For Each TargetCell In ListRange
If TargetCell.Value = Me.ComboBox1.Value Then
'Skip
Else
Me.ComboBox2.AddItem TargetCell.Value
End If
Next TargetCell
End With
End Sub
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!
I have an ActiveX ComboBox1 on Sheet 1 that contains each month. I then have a row on the same sheet that contains a month in each cell Range C7:N7. I would like to write a code that fills the cell in the next row Range(C8:N8) with data from cell D14 on Sheet 2 if ComboBox1 = Range(C7:N7)
The code would look similar to this:
If ComboBox1 = Range(B7, N7) Then
Range(B8, N8) = "Sheet2!$D$14"
End If
Is this possible? Do I need to define something?
For something like this, you can take advantage of the combobox's ListIndex property:
Private Sub ComboBox1_Change()
'Clear prior entries
Range("B8:N8").ClearContents
'Make sure something has been selected in the listbox
If ComboBox1.ListIndex = -1 Then Exit Sub
'Populate the appropiate cell
Range("B8").Offset(, ComboBox1.ListIndex).Value = Sheets("Sheet2").Range("D14").Value
End Sub