Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-07-02
5
Medium Priority
?
1,981 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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