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.
Related
I have created a drop-down menu and am trying to use VBA so that when certain menu items are selected, empty rows with populate beneath the menu item. The number of empty rows will be dependent on the menu item chosen.
Thanks for the help!
Try use "change" activeX event, which can be added to your sheet code where your comboBox are in.
i.e:
Something like this:
Private Sub comboBoxName_change()
Select case comboBoxName.list(comboBoxName.listIndex)
case "a"
'your code to populate rows
case "b"
'your code to populate rows
case "c"
'your code to populate rows
case [...]
end Select
end sub
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.
I have a combo box and a datagridview. I am importing an excel file into the datragridview and in one of the rows in the datagridview, in the third row, it should be a string that is one of the options for the selections in the combo box which was previously mentioned.
This is my current thought process right now .. I think I am on the right path?
Can't figure out if there is a string.compare procedure like in C++
For Each row As DataGridViewRow In datagridview.Rows
If (Datagridview1.Rows(2).cells(0).Value.ToString().Contains(Combobox.Text) Then
Msgbox("they are the same value")
End If
Next
Your code has it a bit backwards
If cboName.Items.Contains(Datagridview1.Rows(2).cells(0).Value.ToString()) Then
cboName is the Name of your ComboBox.
You could do it like this too:
For y=0 to DataGridView1.Rows.Count-1 'Loops trough rows
If Combobox1.Items.Contains(DataGridView1(0,y).Value.ToString()) Then 'Checks if combobox has something named the same way as cell
Msgbox("they are the same value")
End If
Next
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?
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