Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-07-02
5
Medium Priority
?
2,006 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 2000 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

971 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