I am new to VBA and I apologies for bad coding practice in advance.
I need help to sort out the options from my ComboBox, but do not want it to affect the data from excel (like sorting the data on excel too). I was able to get it to only display unique values from my data with the coding below. I was hoping to sort the options from the ComboBox from a-z and numerically. Thanks!
I need help to sort out the options from my ComboBox, but do not want it to affect the data from excel (like sorting the data on excel too). I was able to get it to only display unique values from my data with the coding below. I was hoping to sort the options from the ComboBox from a-z and numerically. Thanks!
Code:
Private Sub UserForm_Initialize()
Dim Motors() As Variant
Dim lastrow As Integer
Dim Check As Boolean
Dim a As Integer
Dim b As Integer
Dim i As Integer
Dim j As Integer
'Finding last row
lastrow = Sheet1.Cells(2, 1).End(xlDown).Row
L = lastrow - 1
ReDim Motors(L, 28) As Variant
For i = 1 To L Step 1
For j = 1 To 28 Step 1
Motors(i, j) = Sheet1.Cells(1 + i, 1 + j)
Next j
Next i
For a = 1 To L
Check = True
For b = 0 To Me.MotorBrand.ListCount - 1
If Sheet1.Cells(a + 1, 4).Value = Me.MotorBrand.List(b) Then
Check = False
ElseIf Sheet1.Cells(a + 1, 4).Value = "" Then
Check = False
Exit For
End If
Next b
If Check Then
Me.MotorBrand.AddItem Sheet1.Cells(a + 1, 4).Value
End If
Next a
End Sub
Last edited by a moderator: