- Code: Select all
Private Sub ComboBox1_Change()
Dim vItems As Variant
Dim vTemp As Variant
Dim i As Long
Dim j As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ComboBox2
.Clear
.List = ws.Range("Product1").Value
End With
vItems = Me.ComboBox2.List
For i = LBound(vItems) To UBound(vItems) - 1
For j = i + 1 To UBound(vItems)
If vItems(i, 0) < vItems(j, 0) Then
vTemp = vItems(i, 0)
vItems(i, 0) = vItems(j, 0)
vItems(j, 0) = vTemp
End If
Next j
Next i
Me.ComboBox2.List = vItems
End Sub
I also Try this which is almost the same code both do the same thing.
- Code: Select all
Dim Ray, i As Integer, j As Integer, temp As String
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ComboBox2
.Clear
.List = ws.Range("Product1").Value
End With
With ComboBox2
Ray = Application.Transpose(.List)
For i = 1 To UBound(Ray) - 1
For j = i To UBound(Ray)
If Ray(j) > Ray(i) Then
temp = Ray(i)
Ray(i) = Ray(j)
Ray(j) = temp
End If
Next j
Next i
.List = Ray
End With