vba- Userform with three comboboxes - excel

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.

Related

Call a hiding macro with a checkbox

I wanted to make a checkbox, calling a macro that hides and unhides columns on Excel worksheet with specific value in cell, but it is not working
I tried the following VBA script
Sub Hide_Forecasts()
Dim c As Range
For Each c In Range("E12:CF12").Cells
If c.Value = "Forecast" Then
c.EntireColumn.Hidden = True
End If
Next c
End Sub
Sub Unhide_Forecasts()
Dim c As Range
For Each c In Range("E12:CF12").Cells
If c.Value = "Forecast" Then
c.EntireColumn.Hidden = False
End If
Next c
End Sub
Sub CheckBox_For()
If CheckBox1.Value = True Then
Call Hide_Forecasts
Else
Call Unhide_Forecasts
End If
End Sub
Please help me out
You haven't said what type of checkbox you're using - Form Control or ActiveX Control.
For an ActiveX Control right-click the control and select View Code.
Use this code that sits behind the worksheet (CheckBox1 will be named after your checkbox).
Private Sub CheckBox1_Click()
Dim Cell As Range
For Each Cell In Me.Range("E12:CF12")
If Cell.Value = "Forecast" Then
'Checkbox returns TRUE/FALSE - Hidden takes TRUE/FALSE so just connect the two up.
Cell.EntireColumn.Hidden = Me.CheckBox1.Value
End If
Next Cell
End Sub
For a Form Control right-click the control and select Assign Macro.
Place this code in a normal module.
Sheet1 is the codename for the sheet (that's the name not in brackets in the Project Explorer).
'Form Control can have three values:
' 1 = Checked
' -4146 = Unchecked
' 2 = Mixed - ignoring that this value may occur.
Public Sub Checkbox_For()
Dim ChkValue As Boolean
'Is value different from -4146? Returns TRUE = Checked or Mixed / FALSE = Unchecked
ChkValue = Sheet1.Shapes("Check Box 1").OLEFormat.Object.Value <> -4146
Dim Cell As Range
For Each Cell In Sheet1.Range("E12:CF12")
If Cell.Value = "Forecast" Then
Cell.EntireColumn.Hidden = ChkValue
End If
Next Cell
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

Dynamic Filter TextBox from ListBox Values

I have populated a column range cells into a UserForm ListBox (below is the code bulk). Now I want to create a TextBox in the same Form to dynamically filter the contents of that ListBox as I type my entry. How can I utilize the AutoFilter method (or other solution) to call the ListBox content? Thanks for your help.
Set rSource = Sheets("Property").Range(Range("B5"), Range("B5").End(xlDown))
ListBox1.List = rSource.Cells.Value
May by something like this help you:
When TextBox is changed it clearing Listbox1 and check which value from list contain that string if yes then macro add it to ListBox1
Private Sub TextBox1_Change()
Dim cell As Range
Set rSource = Sheets("Property").Range(Range("B5"), Range("B5").End(xlDown))
ListBox1.Clear
For Each cell In rSource
If InStr(cell.Value, TextBox1.Text) <> 0 Then
ListBox1.AddItem cell.Value
End If
Next cell
End Sub
Private Sub UserForm_Activate()
Set rSource = Sheets("Property").Range(Range("B5"), Range("B5").End(xlDown))
ListBox1.List = rSource.Cells.Value
End Sub

How do I set up a ComboBox to equal a certain Column for a UserForm?

I am still pretty new to VBA but I am creating a UserForm that will input data in. But I also want to have a ComboBox in the UserForm representing the months. And each month suppose to represent a column. For example, if I pick January on the UserForm, then it will put data on the K column.
I want 1 to 12 to represent Column I to T
Private Sub CommandButton1_Click
Dim ws As Worksheet
Dim lrCal As Long
lrCal = sheets("TestCal").Cells(7,Columns.Count).End(xlToLeft).Column + 1
With sheets("TestCal")
.Cells(7, lrCal).Value = tbApple.Text
.Cells(8, lrCal).Value = tbOrange.Text
.Cells(12,lrCal).Value = tbBread.Text
.Cells(13,lrCal).Value = tbJam.Text
End With
End Sub
Right now it is going to the next available cell. But I want it to go to the column that I pick.
Private Sub cboMonth_Change()
dim cboMonth as ComboBox
dim i as long
dim ws as worksheet
Set ws = ThisWorkbook.sheets("TestCal")
For i = 1 to 12
Next
I am stuck on this part, do not know how to set it up. How would I set up each number to represent each column?
Depending on your approach to populating your combobox, there are numerous ways to do this... one example would be to populate your combobox so that you can relate the list-position to a column, e.g.:
Option Explicit
Private Sub CommandButton1_Click()
Debug.Print Application.Match(ComboBox1.Value, ComboBox1.List, 0)
End Sub
Private Sub UserForm_Initialize()
With Sheets(1)
Me.ComboBox1.List = Array("Jan","Feb","Mar")
End With
End Sub
You can test the above and see that the List position (the location number in the array) is displayed in the immediate window.
So if January is in column 11 (K), you can output your data like:
Private Sub CommandButton1_Click()
'11-1 = 10, you would need to find last row in column for this:
Sheets(1).Cells(LastRow+1,10+Application.Match(ComboBox1.Value, ComboBox1.List, 0)).value = Me.TextBox1.Value
End Sub
You could also do this without the combobox using Match() or Find().

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)

Resources