Help With Recorded Macro

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
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 can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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