How to make a reset button in excel - vba

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

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

Getting a Value if a Field and Record number is known?

Good Evening,
I am working on a Combo Search Form that is designed to search for information by criteria. The form has a combo box containing field values and a text box beside it. The selection of a field value in the combo box will fill in the text box beside it with the relevant information for that record, all the relevant information is contained in the PetTable.
I have managed to get the combo box to display the fields from the PetTable by setting the rowSource to PetTable and the sourceType to Field List... however that's where I hit my dead end.
In the Text Box beside the combo-box I tried grabbing the value of the combo box and putting it into the textbox by making the Text box control source "=ComboBox", however this just created a textbox which has a literal text string to that of the combo box.
My next thought was to make the text box Control source "=PetTable.PetComboBox" my thought was that the PetTable references the table with my information and the "PetComboBox" becomes the field a need to get. This did not work either and gave a #Name error"
What should be happening is: In the Combo-box if I selected [Pet Name], I would hope that the textbox beside it becomes "Fido" but instead it also becomes [Pet Name].
Any and all help would be appreciated!
Thanks
Desired Effect
What you need to do is to change the Row Source Type of the Combo Box to "Table/Query". Then in the "Row Source" click on the "..." to open up the Query Builder. Select the table that you want. Add the columns that you want. I would suggest the table's primary key PetID, and then any other fields - in your case at least PetName. You may also want to sort by PetName to make it easier for the user to scroll through. Close the Query Builder and save the changes. Change the combo box's ColumnCount to 2, and set the Column Widths to be "0cm;6cm" (setting the first column to have a width of 0 means that it is not displayed to the user).
Now move to you TextBox, and set the Control Source to be:
=[Combo0].Column(1)
Note that columns in a combox box are 0-indexed, so the first column is column 0, the second (in your case containing PetName) is column 1.
As you actually want to show the field names, rather than the data in the combo box, then you will need to set the RowSourceType to be "Field List", and then select the table name as the RowSource.
You will then need a small piece of VBA to lookup the value of that field in the table for the current record:
Sub sListFieldData()
If Not IsNull(Me!Combo0) Then
Me!Text2 = DLookup(Me!Combo0, "tblPet", "PetID=" & Me!PetID)
Else
Me!Text2 = ""
End If
End Sub
And you will then need to call this procedure in the combo box's AfterUpdate event (to catch when it has been changed by the user) and also in the form's Current event (to catch when the user moves between records):
Private Sub Combo0_AfterUpdate()
Call sListFieldData
End Sub
Private Sub Form_Current()
Call sListFieldData
End Sub
Regards,

Populating a dynamically added listbox from dynamically added combobox selection

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.

Adding unique records to a listbox from another listbox

I am hoping someone can help me out because I don't often code in Excel VBA. I have two Listbox and a CommandButton on a UserForm, which adds selected records from the first ListBox to the second. The range in the first ListBox is kind of long and so users of the form will generally select a couple records, hit the Add button, and then scroll down the first ListBox to select more records to add. The problem is, only the ones that have been added last will actually be recognized and used to generate reports even though all of the selected records appear in the second ListBox. Here is my current code for the button that adds the records to the second ListBox:
Private Sub AddButton_Click()
For i = 0 To Listbox1.ListCount - 1
If Listbox1.Selected(i) = True Then Listbox2.AddItem Listbox2.List(i)
Next i
End Sub
How can I populate the second ListBox so that it will include every record selected no matter how many times the add button is used?

Link cell value to form combobox value

Might be easy, but how do I link a form comboxbox value to a cell or a dropdown list of cells ? In my case a 1 or 2 dimensional with month - year or month txt - month nr value or array needs to be linked to a form combobox.
The result should be that when I change the form combobox value it also updates the excel cell or cells (the 2 cells combination above) and also the other way around, when I update the cell or cells, it also updates the form combobox value.
Atm, I only have it one way, from form combobox to value by using the built in sub.
Sub ComboBox1_Change()
ThisWorkbook.Sheets("List").Range("G2").value = Form1.ComboBox1.Value
End Sub
After some digging around, I have found the answer to be in the Combobox properties window, ControlSource. I put there in my case 'List'!G2 and combined with the Sub ComboBox1_Change() it updates the month both ways.