Help With Recorded Macro

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,488
Office Version
  1. 365
Platform
  1. Windows
I recorded this Macro below on a file that removes filter then sorts by column C then A (has a title row in 1). I want to use it on files with different amount of rows, what needs changing please to accommodate this?

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Cells.Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
        "C2:C42906"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
        "A2:A42906"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AY42906")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,614
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub Dazzawm()
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
   End With
   With Range("A1:AY" & Range("A" & Rows.Count).End(xlUp).Row)
      .Sort Range("C1"), xlAscending, Range("A1"), , xlAscending, , , xlYes
   End With
End Sub
 

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,488
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub Dazzawm()
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
   End With
   With Range("A1:AY" & Range("A" & Rows.Count).End(xlUp).Row)
      .Sort Range("C1"), xlAscending, Range("A1"), , xlAscending, , , xlYes
   End With
End Sub
Thanks
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,614
Office Version
  1. 365
Platform
  1. Windows
You're welcome.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,682
Messages
5,637,769
Members
416,982
Latest member
lisam77

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
Top