Excel VBA Userform with combo box and Vlookup - excel

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.

Related

Multiple selection based on checkboxes

I have a UserForm where I have 6 check boxes. The role of check box is to select a unique range in Excel. Each checkbox corresponds to a different range in the Excel sheet.
I want to know what methodology can you use to ensure that when a user select a combination of checkboxes max of 6, Excel selects the corresponding ranges of the selected checkbox.
For example:
Checkbox1 programmed to select range A1
Checkbox2 programmed to select range H3
Checkbox3 programmed to select range F6
If User ticks Checkbox1 and Checkbox2 then how can you tell Excel to select A1 and H3 without using If statements since the combination of 6 check boxes would mean a lot of If statements.
Is there anyway when Checkbox1 is selected it keeps that selection in memory and adds it to the next selection.
Thanks
You would loop over the checkboxes, and build a range using Application.Union() (plenty of examples of that here on SO). When you're done looping then select the built-up range.
Or you can build a string like "H3,F6" and use Range(rangeString).Select
For example:
Sub CheckSelection()
Dim s As String, i As Long, sep
For i = 1 To 6
With Me.Controls("Checkbox" & i)
If .Value = True Then
s = s & sep & .Tag 'ranges are stored in checkboxes' Tag property
sep = ","
End If
End With
Next i
If Len(s) = 0 Then s = "A1" 'default selection if none chosen...
ActiveSheet.Range(s).Select
Debug.Print s
End Sub
Private Sub CheckBox1_Click()
CheckSelection
End Sub
'...
' etc
'...
Private Sub CheckBox6_Click()
CheckSelection
End Sub

How to populate a listbox based on a conditional statement

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!

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

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.

If Then Statement in Excel VBA for set range

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

Resources