LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default sort data automatically by the last column in the table

Option Explicit
Private Sub CommandButton1_Click()
Dim myRngToSort As Range
Dim LastRow As Long
Dim LastCol As Long
With Me
'I used column A to find the last used row
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'I used row 1 to find the last used column
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set myRngToSort = .Range("A1", .Cells(LastRow, LastCol))
End With

With myRngToSort
.Sort key1:=.Columns(.Columns.Count), order1:=xlAscending, _
header:=xlYes
End With

End Sub

Polyanna wrote:

I have a spreadsheet that is updated weekly by adding a two new columns of
figures.
How can I add a command button that will automatically sort by the last
column? I can record a macro to sort by the last column, but then when I add
data the last column is not the last column anymore (obviously, you may
say!). Also, occasionally when updating, a new row is added to the bottom.


--

Dave Peterson
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort a column automatically Longtime Excel Worksheet Functions 12 March 26th 08 01:30 PM
How import table and have Excel sort automatically? Kristina Margarita Excel Discussion (Misc queries) 0 September 15th 06 05:28 PM
automatically sort a table after changing a cell Aaron Excel Discussion (Misc queries) 1 July 26th 06 06:30 PM
how do I automatically sort a column on entry Michael Excel Discussion (Misc queries) 5 June 19th 06 02:54 PM
how do i automatically sort data in a pivot table Esche Excel Worksheet Functions 1 June 11th 05 12:43 PM


All times are GMT +1. The time now is 01:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"