How can I populate empty rows in Excel using VBA programming? - vba

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

Related

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?

User selects item from drop down box, clicks button, and the selected item populates the last row of a table

In Microsoft word, I have a 3x3 table, a button, and a drop down list.
When I press the button, I want the last row of the first column to be filled with the selected drop down list item. Then, add a row to the table below it.
Currently, I can't even fill values into the table.
ActiveDocument.Tables(15).Rows.Last.Cells.Value = "Hello" returns an error. What can I do?
I managed to select the last row. ActiveDocument.Tables(15).Rows.Last.Select
Now, I need to copy the current value from the drop down list. How do I do that?
Here is one way. It inserts the text after any existing text:
Option Explicit
Sub PopulateTable()
With ActiveDocument
.Tables(1).Rows.Last.Cells(1).Range.InsertAfter .FormFields("DropDown1").Result
End With
End Sub
If you know the specific cell location you could also use:
.Tables(1).Cell(3, 1).Range.InsertAfter .FormFields("DropDown1").Result
References:
1) table-cell-method-word
2) dropdown-object-word
You can loop so see which form fields are present in the Activedocument and get their names as follows:
Sub GetNames()
Dim myField As FormField
For Each myField In ActiveDocument.FormFields
Debug.Print myField.Name
Next myField
End Sub
If you double click on the drop down form control it will open a window where you can view the Bookmark name i.e. the current name for the drop down object.
You can also associate a macro via this route (so could remove need for a button potentially)
Legacy form control:

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.

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