Combobox with out of order values

Leandroarb

Board Regular
Joined
Oct 7, 2014
Messages
157
I'm adding values to a combobox with a loop, it analyzes numerical values that are in ascending order, but when I open combobox the values are out of order, what could be happening?

I'm used this code:

VBA Code:
Private Sub ComboBox1_Enter()
Dim rngCad       As Range
Dim c            As Range
Dim strArray    As String
Dim ArrayCombo As Variant

Set rngCad = Range(Cells(2, 1), Cells(Application.WorksheetFunction.CountA(Sheets("Cadastro").Range("A:A")), 1))
Me.ComboBox1.Clear
For Each c In rngCad
    If Application.WorksheetFunction.CountIf(rngCad, c) > 0 And InStr(strArray, c) = 0 Then
        ComboBox1.AddItem c
        strArray = strArray & c & ", "
        Else
        strArray = strArray
    End If
Next
End Sub

Explanation: There are repeated values in the range that the loop traverses, so I made the code this way so that only unique values were added.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
A strange discovery ... at the very least. :unsure:

When the range sheet is not active in the workbook, the values are out of order.

I added a line for each section, a combobox is filled by activating the spreadsheet where the range is with the values, and so these values are in ascending order.

VBA Code:
Private Sub ComboBox1_Enter()
Dim rngCad       As Range
Dim c            As Range
Dim strArray    As String
Dim ArrayCombo As Variant

Sheets("Cadastro").Select 'This line to active the respective sheet
Set rngCad = Range(Cells(2, 1), Cells(Application.WorksheetFunction.CountA(Sheets("Cadastro").Range("A:A")), 1))
Me.ComboBox1.Clear
For Each c In rngCad
    If Application.WorksheetFunction.CountIf(rngCad, c) > 0 And InStr(strArray, c) = 0 Then
        ComboBox1.AddItem c.Value
        strArray = strArray & c & ", "
        Else
        strArray = strArray
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top