Populating a dynamically added listbox from dynamically added combobox selection - vba

The userform has 3 columns, a combobox (list of vendors), and listbox (list of vendors' product / service) and a textbox with the price of the selected product / service.
Example of menu (3rd column item not added)
The user presses a button to add a row, which populates within a scrollable frame a new row of the 3 columns. My goal is to have the listbox's options change according to the selection in the combobox on that row.
I have a listener (see my previous post on combobox(number)_change() to see how this works) which is the following ...
Private Sub ControlHandlerCollection_ComboBoxChange(ComboBox As MSForms.ComboBox)
MsgBox ComboBox.Name
End Sub
I wrote this to test if the change sub would work for ComboBox #87 for example. However, I don't know how to tell the same numbered listbox to change its list options according to the selection of ComboBox#
If I had a listener specifically for ComboBox1_Change() then I'd simply write the change for ListBox1 within it, but I'm writing this for a dynamic 'infinite' amount of ComboBoxes.
My current plan of attack would be to write within the sub in the code box above a block of code to parse the result of ComboBox.Name to grab the number as the result would be for example "ComboBox15" I could write a variable assigned to "ListBox" + (ComboBox.Name - "ComboBox") to call ListBox(number) however I'm not sure I can actually then call a variably based name of a listbox.

OK my father helped me come to this solution. It's a little static, but it works perfectly for what I need.
Had to edit the vendor data into multiple sheets and used the Define Name tool under Data to name the range. The listbox changes as per the selection of combobox.
Dim listBoxName As String
listBoxName = "myList" & ComboBox.Tag
Dim rangeName As String
rangeName = "company_1"
Select Case ComboBox.value
Case "Company 1"
rangeName = "company_1"
Case "Company 2"
rangeName = "company_2"
End Select
Dim listBox As Control
Set listBox = Me.Controls(listBoxName)
listBox.RowSource = rangeName
The above code was added to the private sub mentioned in the original thread.

Related

VBA ListBox - Selected Value is Returning a Blank

I have a UserForm with some radio buttons and two single column ListBox's. I'm having trouble returning the value from the second ListBox under certain circumstances.
If the user selects one of the radio buttons they get a series of items to select from in the first ListBox. Once they select from the first ListBox, the 2nd ListBox gets populated with items.
If they select the other radio buttons, the two ListBox's just get populated with a single value "Not Applicable" and I'm selecting it straight away.
This is the code I'm using to set the two ListBox's up with "Not Applicable"
ListBox_First.Clear
ListBox_Second.Clear
ListBox_First.List = Array("Not Applicable")
ListBox_First.Selected(0) = True
ListBox_Second.List = Array("Not Applicable")
ListBox_Second.Selected(0) = True
This is my code to get the value selected
Dim firstValue As String
Dim secondValue As String
firstValue = ListBox_First.Value
secondValue = ListBox_Second.Value
firstValue is ok as it equals "Not Applicable", however secondValue is equal to "". When you look at the form, the value in each ListBox looks like it's selected so I don't understand why it's blank. I have checked the ListCount property and each ListBox only has one item so it should be correctly selected.
If I manually select "Not Applicable" in the second ListBox using the mouse is works fine, but I'm trying to avoid the user having to select it when it's the only value.
I don't know if this is a bug or if I've done something wrong with my code.
I'm working with a product called WRQ Reflections.
Caveats of MS Forms Value property
In addition to Dy.Lees valid answer it might be helpful to note that it's preferrable to avoid .Value and to refer e.g. to ListBox2.List(0, 0) instead.
Furthermore note that due to MS help reference at Value property MS Forms
.Value reflects only the "value in the â–ºBoundColumn of the currently selected rows" and that
value cannot be used with a multi-select list box
The value is recognized by adding the setfocus command.
firstValue = ListBox_First.Value
ListBox_Second.SetFocus
secondValue = ListBox_Second.Value

Dynamic conditions related to radio buttons in excel

How to make the conditions of radio button dynamic? for eg. if i have one radio button "yes" in A100 and the condition related to it is appearing in A103. And, if i am inserting the row i want both(button and its value) to get shifted accordingly .
Sub OptionButton21_Click()
Range("A103") = " Not Applicable "
End Sub
Sub DynamicRange()
'Best used when only your target data is on the worksheet
'Refresh UsedRange (get rid of "Ghost" cells)
Worksheets("Sheet1").UsedRange
'Select UsedRange
Worksheets("Sheet1").UsedRange.Select
End Sub
Thanks in advance
Use a named range
1 -Select the target cell (i.e. A103) and give it a name in the "Name Box" on the top-left of the screen. Suppose you chose the name "MyTargetName"
2- Make the control to move with cells.
Right-click the control --> Format control --> Properties --> Move but dont size with cells
3- In VBA code, refer to the target cell by its name instead of its address:
Sub OptionButton21_Click()
Range("MyTargetName") = " Not Applicable "
End Sub
This will make the referenced target shift correctly, along with the control, whenever new rows are inserted.

How to make a reset button in excel

I am trying to make a reset button that will replace any value the user has selected with the value TOTAL inside a number of comboboxes. Using the record macro i selected the combobox but i can't figure out how to insert the value. The following code gives out the 424 error.
ActiveSheet.Shapes.Range(Array("ComboBox2")).Select.Value = TOTAL
the part that i added to the macro is the .Value=TOTAL
Anyone knows what i should do? Please take note that i don't want to clear the comboboxes; I want to give them a specific value.
In case that combo boxes are Form controls use OLEFormat.Object.ListIndex to select the item.
In case that the combo boxes are ActiveX controls use OLEFormat.Object.Object.ListIndex (note in this case the first item in the list has index 0).
Then iterate through all Combo-boxes you want to reset and set ListIndex to index of item "TOTAL". In this example the item "TOTAL" is on the first place in the list so for Form Combo-box (if used) ListIndex = 1 and for ActiveX Combo-box ListIndex = 0. HTH
(You are probably using ActiveX Combo-boxes, but in the example the older Form Combo-boxes are used as well just for the case).
Sub ResetToTotal2()
Dim combos
Dim combo
Dim comboObject
combos = Array("ComboBox1", "ComboBox2", "ComboBox3")
For Each combo In combos
Set comboObject = ActiveSheet.Shapes(combo).OLEFormat.Object
If TypeName(comboObject) = "DropDown" Then
comboObject.ListIndex = 1
Else
comboObject.Object.ListIndex = 0
End If
Next combo
End Sub

Populating Items in Access

I have two dropdown menus on an access form one for the main category and one for a subcategory. Based upon what you select in the main category, the sub-category dropdown should populate accordingly.
Now based on whats selected in the sub-category, a textbox should then populate.
Its been awhile since I've done any work with SQL and even then I didn't use MS access frequently so any help is much appreciated.
Thanks!
So if you have two comboboxes, Combobox1 and Combobox2, and a textbox, set them up like this:
Make the Combobox2 properties like this (change to suit your needs)
Row Source Type = Table/Query
Row Source = SELECT <Fields> FROM <Table> WHERE Crit_Field=[Combobox1]
Combobox1 should have and a Sub for the Change event which tells Combobox2 to populate:
Private Sub Combobox1_Change()
Me.Combobox2.Requery
End Sub
Combobox2 should also have a Sub for its Change event which tells the textbox to populate:
Private Sub Combobox2_Change()
' Stuff that populates the textbox
End Sub

Get dropdown value in VBA and get the name of the dropdown...nowhere to be found?

I created a dropdown by dragging the combo box onto my sheet from the UserForm toolbar. I assigned some values to it from some cells in the book. Now I want some VBA code to access the selected dropdown item's value in the form of a string.
My dropdown contains only text.
Also how do I find the name of this newly created dropdown (it's nowhere in the properties!)?
Dim dd As DropDown
Set dd = ActiveSheet.DropDowns("Drop Down 6")
Set r = Sheet2.Range(dd.ListFillRange)
Set ddValue = r(dd.Value)
NOTES:
DropDown is not a visible class. You
just use it and it works.
To find the name of the dropdown
CONTROL (not userform) just look at
the name box in the top left corner of your screen just above column A.
It says the name of the control when
you right click on your control.-
Sheet2 is where the dropdown list is
populated. So wherever your list data
is.
Hope that helps you all.
Here's how you get the String without needing to know the name:
Dim DD As Shape
Set DD = ActiveSheet.Shapes(Application.Caller)
MsgBox DD.ControlFormat.List(DD.ControlFormat.ListIndex)
This is a clunky way of doing it but it should work:
Dim o As Object
For Each o In Worksheets("Sheet1").Shapes
MsgBox o.Name
Next o
There is also a hidden DropDowns collection member of the Worksheet object that you could iterate over. This will find items inserted from the Forms toolbar but won't find items inserted from the Control Toolbox toolbar
Lance Roberts was almost there. If you don't know the name of the drop down that calls the sub, use this:
Dim dd as DropDown
Set dd=ActiveSheet.Shapes(Application.Caller).OLEFOrmat.Object
Dim ddVal as String
ddVal=dd.List(dd.ListIndex)
I used this to create a generic sub for a form with many drop downs.