Solved

VB in Excel to sort columns/rows

Posted on 2000-03-08
1
401 Views
Last Modified: 2013-12-25
Hello,

     I've got an excel spreadsheet and I wanted to create a VB tool that will sort the spreadsheet automatically as compared to someone manually going to the "auto filter" feature to sort the information.  Is the best method to record a macro and then use that in VB (I don't know how to reference a recorded macro in VB)...or would it be more efficient to code ALL of it (meaning the functions of an "auto filter") in the VB editor.  

     Primarily, there are 3 columns to sort by: group, individual(s), and priority.  The group and priority columns only have one entry per cell, but the "individual(s)" column may have several names per cell (thus the "auto filter" is best used because you can customize it putting any part of a name to sort).

     I wanted to use controls like a drop down box or a combo box to choose the group, individual(s), and priorities and then click on a button that will sort the data.

     Does anyone have a couple minutes to help me with a resolution, hints, or code?

Thank you so much for your time!  =)
mlouie23
0
Comment
Question by:mlouie23
1 Comment
 
LVL 1

Accepted Solution

by:
Chrynoble earned 100 total points
ID: 2606396
Truth be told I would use the auto filter that is provided by Excel. There are a few reasons for this, one is that you don't have to send macros along with the spread sheet. This will prevent errors when users click "Do not open with macros". I know several users that do this out of habit because of a fear of viruses. Not using VBA will also cut down on the size.

That being said, there are reasons to add a macro. I would however recommend against using the macro recorder for something like this as it isn't very flexible.

If you add a combo box, and a command button to the worksheet here is some code that will sort the data.
(Make sure the lines show up right after cutting and pasting.)

ThisWorksheet Module in the VB Editor:
Private Sub Workbook_Open()
    Worksheets("THE NAME OF YOUR SHEET HERE").Activate
    Worksheets("THE NAME OF YOUR SHEET HERE").ComboBox1.AddItem "individual(s)"
    Worksheets("THE NAME OF YOUR SHEET HERE").ComboBox1.AddItem "group"
    Worksheets("THE NAME OF YOUR SHEET HERE").ComboBox1.AddItem "priority"
End Sub

Sheet(Sheet?) Module in the VB Editor:
Private Sub CommandButton1_Click()
    Dim SortCol As String
    'Make sure your combobox doesn't support multiselect before running this.
    SortCol = Worksheets("THE NAME OF YOUR SHEET HERE").ComboBox1.Value
    Range("A1").Select ' this should be the top left cell in the range to be sorted.
    'Notice the use of SortCol to identify the sort key
    Selection.Sort Key1:=SortCol, Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

Email if you need more help
Chrynoble@hotmail.com
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

24 Experts available now in Live!

Get 1:1 Help Now