Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

708 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

19 Experts available now in Live!

Get 1:1 Help Now