Using VBA, I'm trying to build this sophisticated form to add new 开发者_StackOverflow中文版rows to an existing table. I want to have this ComboBox that will list the values already exists in the one of the table's column.
I'm new to VBA. and I tried some Range/Selection and sort combinations with no lack so far...
Here's what you're looking for.. It should get you started, Just adapt the Sheets and Range to your needs.
Dim cmb as ComboBox
Dim rng as Range
Set cmb = Worksheets("Sheet1").ComboBox1
'To fill based on range
For Each rng in Worksheets("Sheet2").Range("C2:C300")
Cmb.AddItem Rng.Value
Next
'To fill from table where ListColumns(N) is the specific column
Set rng = Sheet2.ListObject(1).ListColumns(3).Range
For Each rng in rng
Cmb.AddItem Rng.Value
Next
Cmb.ListIndex = 0
*EDITED:*Chris is right, my original code had errors. Posted answer on way to work didn't have time to check. The code above works fine. Chris suggestion on just using .value to fill is quicker. I honestly didn't know you could do it like that.
you need to create your table column range,
Either you can insert your row in side the range
Or you need to first add row to table and resize your range
and pass that range pass to SetRng parameter,
userFormName is user form name,
ControlName is combobox name
Public Function FillRangeComboBox(userFormName As String, ControlName As String, SetRng As Range) As Boolean
Dim ObjFormName As Object: Set ObjFormName = ThisWorkbook.VBProject.VBComponents(userFormName)
Dim ObjControlName As MSForms.ComboBox: Set ObjControlName = ObjFormName.Designer.Controls(ControlName)
''set combobox value
With ObjControlName
.RowSource = SetRng.Address
End With
End Function
精彩评论