Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 115
Default Simplfying the Sort macro

I would like to have a macro to sort every column in descending order.
Given below is the the sort macro I have for each and every column.
This implies I have to copy,paste and change the corresponding column
name 256 times.

Is there any simple way to do this? Thanks

Columns("A:A").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

Columns("B:B").Select
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

Columns("C:C").Select
Selection.Sort Key1:=Range("C2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

Columns("D:D").Select
Selection.Sort Key1:=Range("D2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

etc till Column IV

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Simplfying the Sort macro

Try

Sub SortEachColumn()
Dim LastCol As Long
Dim LastRow As Long
Dim N As Long
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For N = 1 To LastCol
LastRow = Cells(Rows.Count, N).End(xlUp).Row
Range(Cells(1, N), Cells(LastRow, N)).Sort _
key1:=Cells(1, N), order1:=xlAscending
Next N
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


wrote in message
ps.com...
I would like to have a macro to sort every column in descending order.
Given below is the the sort macro I have for each and every column.
This implies I have to copy,paste and change the corresponding column
name 256 times.

Is there any simple way to do this? Thanks

Columns("A:A").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

Columns("B:B").Select
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

Columns("C:C").Select
Selection.Sort Key1:=Range("C2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

Columns("D:D").Select
Selection.Sort Key1:=Range("D2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

etc till Column IV


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 115
Default Simplfying the Sort macro

On Oct 22, 5:10 pm, "Chip Pearson" wrote:
Try

Sub SortEachColumn()
Dim LastCol As Long
Dim LastRow As Long
Dim N As Long
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For N = 1 To LastCol
LastRow = Cells(Rows.Count, N).End(xlUp).Row
Range(Cells(1, N), Cells(LastRow, N)).Sort _
key1:=Cells(1, N), order1:=xlAscending
Next N
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consultingwww.cpearson.com
(email on the web site)

wrote in message

ps.com...



I would like to have a macro to sort every column in descending order.
Given below is the the sort macro I have for each and every column.
This implies I have to copy,paste and change the corresponding column
name 256 times.


Is there any simple way to do this? Thanks


Columns("A:A").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal


Columns("B:B").Select
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal


Columns("C:C").Select
Selection.Sort Key1:=Range("C2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal


Columns("D:D").Select
Selection.Sort Key1:=Range("D2"), Order1:=xlDescending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal


etc till Column IV- Hide quoted text -


- Show quoted text -


Chip, your code works great. Thank you very much. Here is the code I
am using to suit my need

Sub sort_each_column()
Range("A1:IV65536").Value = ""
Sheets("Sheet1").Range("A1:IV65536").Copy
Sheets("Sheet4").Select
Range("A1").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

Dim LastCol As Long
Dim LastRow As Long
Dim N As Long
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For N = 1 To LastCol
LastRow = Cells(Rows.Count, N).End(xlUp).Row
Range(Cells(1, N), Cells(LastRow, N)).Sort _
key1:=Cells(1, N), order1:=xlDescending
Next N

Range("A1").Select

End Sub

Reply
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
Simplfying DIV/0 Error...Can you? Neil M Excel Worksheet Functions 4 June 24th 08 09:36 PM
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
Using Macro to sort without clicking on macro button dd Excel Discussion (Misc queries) 3 May 3rd 07 06:00 PM
Sort Macro kronik Excel Programming 0 March 13th 06 04:55 AM
Sort Macro Sprinks Excel Discussion (Misc queries) 1 April 19th 05 04:58 PM


All times are GMT +1. The time now is 07:43 AM.

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

About Us

"It's about Microsoft Excel"