Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Jim Jim is offline
external usenet poster
 
Posts: 615
Default Sort by column, not range

I'm trying to set up a macro that can be used from multiple workbooks with
the same columns but different worksheet names. The number of rows will also
vary. How can I reference the column rather than the range? I want it to
sort by the Customer column which is the column header for column A. Below
is my current code which references a specific worksheet name and range
(which I'm trying to get away from). I recorded the macro for this code.

Columns("A:A").Select
Selection.FormatConditions.AddUniqueValues

Selection.FormatConditions(Selection.FormatConditi ons.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("A1").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear

ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add(Range("A2:A251"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color =
RGB(255, 0 _
, 0)
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=Range( _
"A2:A251"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:V251")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub


Thank you,
Jim
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Sort by column, not range

Well, here's a kind of 'generic' or skeleton for a routine that will work on
the current active sheet and you define what columns to include in the sort.
As set up it's just sorting 1 column, but can handle several with one change,
and could handle multiple 'keys' with minor modification. It'll work with
Excel 2007 or 2003. And since I've done away with the DataOption:= entries,
it'll even work probably as far back as Excel 2000. The 2007 macro recorder
sure adds a lot of stuff that (my humble opinion) just isn't needed to just
accomplish the sort.

There are more possible variations to this than there is room for describing
them here. For example, if I wanted to run the same sort on every sheet in a
workbook, instead of referencing the ActiveSheet, I could add a little code
like this:

Dim anySheet As Worksheet

For Each anySheet in ThisWorkbook.Worksheets
'and put the setup and actual sort code in here
'that is all but the declarations
'and work through all sheets
' just change ActiveSheet. to anySheet. in the
' code below and it would work
Next ' end anySheet loop

Sub SortingSkeleton()
'change these constants as desired, if both
'first and last column to sort are the same, then
'just a single column will be sorted
'the "keyColumn" is a column within the
'sorted ones that the sort is to be sorted by
'if you're writing a 1-column sort, then
'all 3 columns would be the same
Const firstColToSort = "A"
Const lastColToSort = "A"
Const keyColumn = "A"
Const firstRowToSort = 2 ' assumes labels in row 1

Dim sortRange As Range
Dim sKey1 As Range
Dim lastRow As Long

'works on the currently selected/active sheet
lastRow = ActiveSheet.Range(keyColumn & _
Rows.Count).End(xlUp).Row
Set sortRange = ActiveSheet.Range(firstColToSort & _
firstRowToSort & ":" & lastColToSort & lastRow)
Set sKey1 = ActiveSheet.Range(keyColumn & firstRowToSort)

'you can record macros while performing other sorts,
'such as on multiple columns with 1, 2 or 3
'sort keys and modify this code to emulate them
'based on the recorded macros
sortRange.Sort Key1:=sKey1, Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom

'housekeeping
Set sortRange = Nothing
Set sKey1 = Nothing
End Sub


"Jim" wrote:

I'm trying to set up a macro that can be used from multiple workbooks with
the same columns but different worksheet names. The number of rows will also
vary. How can I reference the column rather than the range? I want it to
sort by the Customer column which is the column header for column A. Below
is my current code which references a specific worksheet name and range
(which I'm trying to get away from). I recorded the macro for this code.

Columns("A:A").Select
Selection.FormatConditions.AddUniqueValues

Selection.FormatConditions(Selection.FormatConditi ons.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("A1").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear

ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add(Range("A2:A251"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color =
RGB(255, 0 _
, 0)
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=Range( _
"A2:A251"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:V251")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub


Thank you,
Jim

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
Unable to sort when column is included in range Trudy Excel Discussion (Misc queries) 1 July 8th 08 03:27 PM
Sort a range of data based on another column RobM Excel Discussion (Misc queries) 1 July 13th 07 05:36 AM
formula to match a range(sort) so it matches an external column steveo Excel Discussion (Misc queries) 1 June 26th 06 07:54 AM
formula to sort a range so that it matches the exact rows of a column that is outside that range? steveo Excel Discussion (Misc queries) 1 June 18th 06 02:05 AM
Dynamic column chart - auto sort on data range jimfrog Charts and Charting in Excel 0 March 29th 06 02:45 PM


All times are GMT +1. The time now is 12:42 PM.

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"