Thread: Macro Help
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc
JasonP CCTM LV[_2_] JasonP CCTM LV[_2_] is offline
external usenet poster
 
Posts: 36
Default Macro Help

I create lease payment schedules for different cities. The schedules have 9
columns across, but are varying in the number of rows down, anywhere from 30
to 3000+.

I would like to make a macro that would automatically sort by :

1. if the Payee (who the payment is to) has one line or more than one
(meaning if he is receiving 2 or more payments, move to the bottom), then
2. by the Payee Name, Alice Smith with 4 lines before John Smith with 2 lines
3. then by the payment number (alice smith has 4 payment numbers, john smith
has 2)

I can figure out the macro, but the problem is since the number of rows vary
by schedule and sometimes by month, (New York usually has about 3000, but one
month might be 2500, etc), I cant use the same macro because the cursor
always moves down a specific number of rows.

For Example: If I create the macro using a table of only 300 rows, but then
try to use it on a table of 3000, the macro will only format the first 300
rows.

Can anyone figure out how to make the cursor go to the bottom of the table
instead of the specific number of rows?

Below is the macro:

Sub FleetSort()
'
' FleetSort Macro
'

'
Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D5").Select
ActiveCell.FormulaR1C1 = "=OR(RC[-1]=R[-1]C[-1],RC[-1]=R[1]C[-1])"
Range("C6").Select
Selection.End(xlDown).Select
Range("D101").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Columns("D:D").ColumnWidth = 20.86
Columns("D:D").EntireColumn.AutoFit
Range("D8").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("D5:D101") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("C5:C101") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("H5:H101") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("G5:G101") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("B5:B101") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("B4:I101")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.SmallScroll Down:=-24
End Sub