Configure sort for variable row count

rebubula

New Member
Joined
Dec 20, 2010
Messages
26
Hi. I would like this macro to allow sorting for files with a variable row count. How do I fix the range?

Code:
Sub Proofing()
'
' Proofing Macro
' Arrange columns for the proofing process
'
' Keyboard Shortcut: Ctrl+Shift+W
'
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Data Check"
    Columns("E:E").Select
    Selection.Delete Shift:=xlToLeft
    Columns("H:J").Select
    Selection.Delete Shift:=xlToLeft
    Columns("I:L").Select
    Selection.Delete Shift:=xlToLeft
    Columns("J:T").Select
    Selection.Delete Shift:=xlToLeft
    Columns("S:S").Select
    Selection.Cut
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Columns("P:T").Select
    Selection.Delete Shift:=xlToLeft
    Columns("N:O").Select
    Selection.Cut
    Columns("C:D").Select
    ActiveSheet.Paste
    Columns("E:E").Select
    Selection.Cut
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight
    Columns("L:L").Select
    Selection.Delete Shift:=xlToLeft
    Columns("K:L").Select
    Selection.Cut
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Cells.Select
    Cells.EntireColumn.AutoFit
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "B2:B6641"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "C2:C6641"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "D2:D6641"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "F2:F6641"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "G2:G6641"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "E2:E6641"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Proofing").Sort
        .SetRange Range("A1:AQ6641")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub


Thanks in advance!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Make a backup copy first and then test this:
Code:
Sub Proofing()
'
' Proofing Macro
' Arrange columns for the proofing process
'
' Keyboard Shortcut: Ctrl+Shift+W
'
    Dim lLastRow As Long
    lLastRow = Range("B" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").FormulaR1C1 = "Data Check"
    Columns("E:E").Delete Shift:=xlToLeft
    Columns("H:J").Delete Shift:=xlToLeft
    Columns("I:L").Delete Shift:=xlToLeft
    Columns("J:T").Delete Shift:=xlToLeft
    Columns("S:S").Cut
    Columns("B:B").Insert Shift:=xlToRight
    Columns("P:T").Delete Shift:=xlToLeft
    Columns("N:O").Cut
    Columns("C:D").Select
    ActiveSheet.Paste
    Columns("E:E").Cut
    Columns("C:C").Insert Shift:=xlToRight
    Columns("L:L").Delete Shift:=xlToLeft
    Columns("K:L").Cut
    Columns("B:B").Insert Shift:=xlToRight
    Cells.Select
    Cells.EntireColumn.AutoFit
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "B2:B" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "C2:C" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "D2:D" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "F2:F" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "G2:G" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Proofing").Sort.SortFields.Add Key:=Range( _
        "E2:E" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Proofing").Sort
        .SetRange Range("A1:AQ" & lLastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top