Solved

VB in Excel to sort columns/rows

Posted on 2000-03-08
1
428 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
[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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

729 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