You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
Hi, I am new to VBA in Excel although I have some basic experience with VB.NET.
I am trying to sort columns A to E on a worksheet when a button is clicked on this worksheet. The columns contain 3960 rows of data. Row 1 contains the headers for the columns and rows 2 through 3961 contain the data itself.
Please could anyone tell me how to write the VBA onClick function for the button so that the data cells in these columns are sorted by:
Column A
(Last Name)
Decending Order
then
Column B
(First Name)
Decending Order.
Thanks in advance for any help or comments you may be able to provide me. :)
__________________
-------------------- James Sellwood
--------------------
The macro record feature can be a great way to get familiar with Excel VBA, just realize that Excel will put a lot of garbage in the code when you record something, and also the recorded macro is just ONE way of doing something, it may not be the best way...for example, you do not need to SELECT the cells in order to sort them...
Code:
Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
Worksheets("Sheet1").Range("A1:E" & LastRow & ").Sort Key1:=Worksheets("Sheet1").Columns("A"), Order1:=xlDescending, Key2:=Worksheets("Sheet1").Columns("B"), Order2:=xlDescending, Header:=xlYes
You also have to remember when using the macro recorder for Excel to choose whether you want Relative or Absolute Referencing or the macro will not work the way you expect it.
Gregory Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
quote:Originally posted by ~Bean~
The macro record feature can be a great way to get familiar with Excel VBA, just realize that Excel will put a lot of garbage in the code when you record something, and also the recorded macro is just ONE way of doing something, it may not be the best way...for example, you do not need to SELECT the cells in order to sort them...
Code:
Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
Worksheets("Sheet1").Range("A1:E" & LastRow & ").Sort Key1:=Worksheets("Sheet1").Columns("A"), Order1:=xlDescending, Key2:=Worksheets("Sheet1").Columns("B"), Order2:=xlDescending, Header:=xlYes
:)
Thanks, I used the macro recorder to get Excel to tell me how it sorted the data just as I needed. I then used your.
Code:
Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
to dynamically pull out the last row to be sorted.
Great, Thanks very much to all who posted.
--------------------
:) James Sellwood :)
--------------------
Can you explain the below code? I used it and I am getting row 24 (for a sheet with the last data being on row 23) and row 30 (for a different sheet with the last data on row 23).