I wanted to take information from a range and add it to a userform in a combobox.
The below code did that for me with success, but in the combo box I set a range of 40 rows, so it has my values and then after my last value it has an empty space of around 20 lines which I put there in case the user adds more cost centres.
How can the combobox select the exact rows and then recognise, if a new cost centre is added later?
"rules" is the worksheet I get information from, "Cost Centres" is the name of a range I built on that worksheet which has 40 rows.
What is best practice that more than 40 cost centres get added, that I'll never need to go back into the code to change the range?
Private Sub UserForm_Initialize()
Worksheets("Rules").Activate
Dim rangeCount As Integer
Dim Range As Range
Dim i As Integer
Set Range = ActiveSheet.Range("A2")
rangeCount = ActiveSheet.Range("CostCentres").Count
i = 2
Do While i <= rangeCount
Me.CostCentreCMBox.AddItem Cells(i, "a").Text
i = i + 1
Loop
End Sub