Solved

EXcel VBA - Sort an Array in alphabetical order (For listing in combobox)

Posted on 2013-07-02
5
1,890 Views
1 Endorsement
Last Modified: 2013-07-02
Hi Guys,

The code below builds an array of items from a register / log of entries, these are then added as selectable items in a dropdown combobox on worksheet. All works fine, but as the list is getting larger, it's getting more difficult to find the one we want, so I want to make the list alphabetical, the source data is arranged chronologically and must be kept that way, so that option is out, I need to find a way of sorting the data either in the array or in the combobox. Any ideas?


wb = Workbooks.Open(Filename:=sTemp )

wb.Sheets("Projects Log").Select

sSections = ""

If Not myDict1 Is Nothing Then
        myDict1.RemoveAll
    Else
        Set myDict1 = CreateObject("Scripting.Dictionary")
End If
0
For Each r In Range("A6:A1000")
    If r.Value = "" Then
    GoTo Next1
    End If
        If Not myDict1.exists(r.Value & "-" & r.Offset(0, 1).Value) And r.Offset(0, 9).Value = "New" Then
            myDict1.Add r.Value & "-" & r.Offset(0, 1).Value, i
            i = i + 1
            sSections = sSections & "|" & r.Value & "-" & r.Offset(0, 1).Text
        End If
Next r

Next1:

MyArray1 = Split(sSections, "|")

ThisWorkbook.Activate
Sheet1.Select
Sheet1.ComboBox1.List = MyArray1

Open in new window

1
Comment
Question by:Conor_Newman
  • 3
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39292495
You could use ADO to query the source workbook directly. (I'm not sure why  you use the string and split methods here when all the data is in your Dictionary already and can be returned as an array using its Items method)

For example:

   Dim strQuery As String
   Dim rst As Object
   Dim strConn As String
   Const adOpenStatic As Long = 3
   Const adLockReadOnly As Long = 1
   Const adCmdText As Long = 1

   strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sTemp & ";Extended Properties=""Excel 12.0;HDR=No;"""

   strQuery = "SELECT DISTINCT [F1] FROM [Projects Log$A6:A1000] ORDER BY [F1];"

   Set rst = CreateObject("ADODB.Recordset")

   rst.Open strQuery, strConn
   If Not rst.EOF then Sheet1.ComboBox1.Column = rst.GetRows
   rst.close
   set rst = nothing

Open in new window

0
 
LVL 2

Author Comment

by:Conor_Newman
ID: 39292619
Ok, that is so much tidier and simpler. I've never used ADO (All my VBA has been learnt through this site stumbling through picking it up as I go along), but that definitely is alot easier.

So the strQuery is what defines what data is taken? Can I change it to say  ...

"SELECT DISTINCT [F1] & "-" & [F2] FROM [Projects Log$A6:B1000] ORDER BY [F1];"

Open in new window


The Data I want is a Project ID in Column A and a Billing Code in Column B, and they need to be displayed as  XXXXXXX-YYYYY
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 500 total points
ID: 39292638
Almost - it would have to be:

"SELECT DISTINCT [F1] & ""-"" & [F2] FROM [Projects Log$A6:B1000] ORDER BY [F1] & ""-"" & [F2];"

Open in new window


Note the double quotes to escape the quoted string "-" and the ORDER BY field has to match what you selected
0
 
LVL 2

Author Comment

by:Conor_Newman
ID: 39292645
You Sir are a Genius!


Thank you very much.

Although it looks like I now have to get back and recode a system that's been slowly improved and integrated more and more over the last 2 years.. :P After I sit down and learn a bit more about ADO. As I have data going both ways, with multiple users accessing it, sometimes simultaneously, not having it open the excel files every time I want to put data in or take it out with definitely make things run a lot better and faster. Thanks again ;-)
0
 
LVL 2

Author Closing Comment

by:Conor_Newman
ID: 39292649
Excellent solution as always from Rorya. Much appreciated, I've learned a lot today.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you how to use shortcut menus in the Access run-time environment.
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now