Solved

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

Posted on 2013-07-02
5
1,918 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

820 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