Populate Combobox2 Based on Previous Combobox1 Selection - vba

I have two combobox.The first combobox has three items as:{One, Tow, Three} now I would like to load the second combobox based on what user select in combobox1.
For example if user select One in the from combobox1 then namebox1 will populate to combox2
and if if user select Two in the from combobox2 then namebox2 will populate to combox2 and so on..
Can you please let me know how I can do this in VBA?
Thanks
Here is the Update Code:
Private Sub ComboBox1_Change()
Me.ComboBox2.Clear
Select Case Me.ComboBox1.Value
Case "One"
With Me.ComboBox2
.RowSource = "nameBox1"
End With
Case "Two"
With Me.ComboBox2
.RowSource = "nameBox1"
End With
Case "Three"
With Me.ComboBox2
.RowSource = "nameBox1"
End With
End Select
End Sub
Please be advise that I didn't use .addItem to populate the ComboBox1. It hasbeen populate by same method .RowSource which is usinf excel collection selection box

The easiest way to do this would be using a Select Case statement.
Suppose you had
Private Sub ComboBox1_Change()
Me.ComboBox2.Clear
Select Case Me.ComboBox1.Value
Case "One"
' If your data was information you wanted to put in yourself:
With Me.ComboBox2
.AddItem "Adam"
.AddItem "Allen"
.AddItem "Andy"
End With
Case "Two"
' If your data existed in a worksheet range
ComboBox2.RowSource = MyRange.Address
End With
End Select
End Sub
I hope this explains enough to get you going - Otherwise, I'm a bit confused - Can you explain what you mean by NameBox in your question

Related

Save combobox value as global variable and select row of selected value

I want to save selected combobox value as global variable and select row of selected value.
I have an Excel file, where I want to make calculations based on inputs in sheet1 and data on sheet2.
Inputs are provided by combobox1 (list of names from column A in sheet 2), combobox2 (case yes/no) and combobox3 (values 1,2,3).
After I select value in combobox1 (for example: ABC which is value A7 from sheet2), I want to calculate from data in row 7 in sheet2:
B7 (sheet2) + C7 (sheet2) * combobox3 value + D7 (sheet2) * (combobox2(yes = 2 / no = 0).
Can anyone help me with that?
Public SelectedComboBox1 As String
Public SelectedComboBox2 As String
Public SelectedComboBox3 As integer
Public calculate2 As Integer
Private Sub ComboBox1_DropButtonClick()
Sheet1.ComboBox1.List = Sheet2.Range("A3:A46").Value
SelectedComboBox1 = Me.ComboBox1.Value
End Sub
Private Sub ComboBox2_DropButton()
With Me.ComboBox2
.AddItem "YES"
.AddItem "NO"
End With
SelectedComboBox2 = Me.ComboBox2.Value
End Sub
Private Sub ComboBox3_DropButton()
With Me.ComboBox3
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
SelectedComboBox3 = Me.ComboBox3.Value
End Sub
Public Sub Calculate2_click()
calculate2 = Sheet2.Range("B7") * Sheet2.Range("C7") * SelectedComboBox3+Sheet2.Range("D7")*??
Sheet1.Range("H10").Value = calculate2
End Sub
It seems like you added a ActiveX combobox. You might want to use a form-combobox instead for Excel Sheets. Nevertheless: in the Editor you can add varioous actions to events in the combobox. What you did is add a reaction to the button-down-click Event. There you told excel to fill the list and set the SelectedCombobox variable to the - as of yet undefined - value of the combobox.
What you might want is one sub to fill the list as you did and another sub reacting to the change event of the combobox. That sub will be called as soon as someone changes the value of the box:
Private Sub ComboBox1_DropButtonClick()
Sheet1.ComboBox1.List = Sheet2.Range("A3:A46").Value
End Sub
Private Sub ComboBox1_Change()
SelectedComboBox1 = Me.ComboBox1.Value
End Sub
This should get you a good start. There are plenty of resources out-there that teach you how to write effective macros in Excel.
If you just need a result that uses the value of these three comboboxes, you could also connect a simple form-comboboxes with respective cells and claculate the result out of those cells.
But if you still want to use vba, think about using just one button to trigger a sub and access the values immediately:
Private Sub Button1_click
cells("A1").value=Me.ComboBox1.Value * Me.ComboBox2.Value...
End Sub

Excel VBA: Accessing the individual fields in a selected ListBox row

I am trying to access the individual cells in a selected/highlighted ListBox "lstData" row so I can reference their values elsewhere.
When I set a watch for Me.lstData.SelectedItem, I get Expression not defined in context. Same with Me.lstData.SelectedIndex and Me.lstData.Rows(1). The only thing that kind of works for me is Me.lstData.Value, but it ONLY returns the leftmost cell. When I try to plug it into the =OFFSET function
=Offset(Me.lstData.Value, ,1,1)
to access the cell immediately to the right, I get Expression not defined in context again.
How can I reference the other selected cells?
I don't think you can use Offset on a ListBox form control. The way to reference a 'cell' in a multi-column ListBox is by indexing the List property.
Here, i returns the row of the selected item, and 1 represents the second column (base 0) of the listbox:
Option Explicit
Private Sub CommandButton1_Click()
Dim i As Long
With Me.ListBox1
i = .ListIndex
MsgBox .List(i, 1)
End With
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "A"
.List(0, 1) = "Alpha"
.AddItem "B"
.List(1, 1) = "Beta"
End With
End Sub

Filling VBA userform's combobox with table column plus an additional option

I am developing an application, in excel with VBA forms. in one form I have a combobox to that let user select customer name, the rowsource of this combobox is a named range (name column of customers table). Everything working fine but I need to add 1 or more additional items in the combobox that not exist in the table column. For example I need to add "All" item in the cobmobox so user can select a particular customer name or All. at other place I wan't to add "Other" item in combobox with same rowsource so if the customer is new user can select Other and then type name in textbox.
I tried following code to add an item
Private Sub UserForm_Activate()
With Me.testCombo
.AddItem "All"
End With
End Sub
but i got error
Run-time error '70'
permission denied
if i remove rowsource property from the combobox then the above code work but only one item "All" display.
Note: I don't want to add "All" and "Other" in customer table, this could be easy solution but will cause other problem.
Try like this:
Private Sub UserForm_Activate()
Dim rowValue As Variant
Dim lngCount As Long
Dim myCell As Range
Dim varCombo() As Variant
With Me.ComboBox1
ReDim varCombo(Me.ComboBox1.ListCount)
For Each myCell In Range(.RowSource)
varCombo(lngCount) = myCell.value
lngCount = lngCount + 1
Next myCell
.RowSource = ""
For lngCount = LBound(varCombo) To UBound(varCombo) - 1
.AddItem CStr(varCombo(lngCount))
Next lngCount
.AddItem "All"
.AddItem "Nothing"
End With
End Sub
As mentioned in the comments, by A.S.H., you should unset the .RowSource property. However, you do not lose it, if you run the code twice, it would be the same. In my code I use UBound(varCombo) - 1, because I use lngCount=lngCount+1 on the last looping over the cell.
Something like this could do what you need
Dim a() As Variant
Dim b() As String
Dim s As String
a = Application.Transpose(Range("a1:a5").Value)
s = "Please select;" & Join(a, ";")
Erase a
b = Split(s, ";")
Me.ComboBox1.List = b
Thank you everyone for helping, the main problem was permission as A.S.H said if Rowsource is set then cannot add any item in the ComboBox. So I delete the RowSource from the properties in form. and wrote following code and it seems everything working fine. I hope my codes are good enough and simple.
Private Sub fillComboBox()
Dim comboData As Range
With Me.CWR_CustName
' first option of comobobox will be All
.AddItem "All"
For RW_Cust = 1 To Range("tblCust").Rows.Count
' add each customer name from customer table name column
.AddItem (Range("tblCust[Name]")(RW_Cust))
Next RW_Cust
End With
End Sub

VBA code simplification

I have the following macro
Private Sub ComboBox1_Change()
If ComboBox1 = "Berlin" Then
Range("C20").Activate
Else
If ComboBox1 = "Paris" Then
Range("C100").Activate
Else
If ComboBox1 = "London" Then
Range("C150").Activate
End If
End If
End If
End Sub
This macro takes the value from a dropdown menu and goes to the cell, where the value is. First question is:
How can I take the values from the cells and not write them specifically in the code?
Second question is:
How can I simplify the procedure and not write for every value an IF?
First, you probably don't actually want to Activate the range! See this answer:
How to avoid using Select in Excel VBA macros
Secondly, your code...
Your Code
Private Sub ComboBox1_Change()
If ComboBox1 = "Berlin" Then
Range("C20").Activate
Else
If ComboBox1 = "Paris" Then
Range("C100").Activate
Else
If ComboBox1 = "London" Then
Range("C150").Activate
End If
End If
End If
End Sub
Using ElseIf
Private Sub ComboBox1_Change()
If ComboBox1 = "Berlin" Then
Range("C20").Activate
ElseIf ComboBox1 = "Paris" Then
Range("C100").Activate
ElseIf ComboBox1 = "London" Then
Range("C150").Activate
End If
End Sub
See documentation: https://msdn.microsoft.com/en-us/library/office/gg251599.aspx
Not hard-coding the values
Private Sub ComboBox1_Change()
Dim rng as Range
Set rng = Nothing
Set rng = ActiveSheet.Range("C:C").Find(ComboBox1.Text)
If Not rng Is Nothing Then
' As I say, you probably don't actually want to use Activate!
rng.Activate
End If
End Sub
See more about the Range object here:
https://msdn.microsoft.com/en-us/library/office/ff838238.aspx
It has useful methods like Address or Value for common use in VBA. The Find function returns a Range object or Nothing if the given value isn't found in the given range.
How can I simplify the procedure and not write for every value an IF?
If you need to test your ComboBox repeatedly (like your If-ElseIf structure), you can use the SELECT CASE to simplify your code:
Private Sub ComboBox1_Change()
Select Case ComboBox1
Case Is = "Berlin"
Range("C20").Activate
Case Is = "Paris"
Range("C100").Activate
Case Is = "London"
Range("C150").Activate
Case Else
Range("C1").Activate
End Select
End Sub
This looks at the value of ComboBox1 and picks the appropriate section to run. For example, if ComboBox1 = "Paris", it skips to the "Paris" case and only runs that section (Range("C100").Activate).
This makes it much easier to add more items to your options, and it reduces the clutter of lots of If-ElseIf-Else lines.
Edit: As mentioned by Wujaszkun, adding the Case Else section handles a ComboBox1 value that was not one of the specified cases.

VBA: TextBox and ListBox

On my UserForm I have a TextBox where is write down my Serial Numbers, something like 421012.
Below it I have a ListBox with options like "Tools, Bench, Wheel"
What I'm attemting to do:
After I type my Serial that I just typed in, I click on one of the options in the ListBox and it should put a value behind my Serial.
Something like this: 421012 + "selecting Tools" =42101215
the "Bench" would get an result of 42101245
My code so far:
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "Tools"
.AddItem "Bench"
.AddItem "Wheel"
End With
End Sub
Somehow I have to tell VBA that Tools equals 15 or Bench equals 45 etc.etc.
Add a second column and then reference that when you're doing your concatenation of the value. "42012" & me.listbox.column(1) should return the value of whatever is selected
with listbox
.columncount = 2
.additem
.list(1,0) = "Tools"
.list(1,1) = 15
.list(2,0) = "Bench"
.list(2,1) = 45
.list(3,0) = "Wheel"
.list(3,1) = 75
end with
I use this much more frequently with objects than with intrinsic types, but another option is to store a Dictionary containing lookups keyed on your ListBox items:
'Module level variable
Private SerialLookup As Scripting.Dictionary
Private Sub UserForm_Initialize()
Set SerialLookup = New Scripting.Dictionary
With SerialLookup
.Add "Tools", "15"
.Add "Bench", "45"
.Add "Wheel", "42"
End With
ListBox1.List = SerialLookup.Keys()
End Sub
Then you can retrieve whatever value you need by using it as an index into the Dictionary, i.e.:
TextBox1.Value = "421012" & SerialLookup.Item(ListBox1.Value)