How to change code to apply to all files

UzieJacuzzi

New Member
Joined
Jul 17, 2012
Messages
8
Hello everyone,

I have a macro i recorded for the following:

Sort
Autofit Contents
Align Left

Here is the code:

Code:
Sub Uzie_2_SortTidyUp()
'
' Uzie_2_SortTidyUp Macro
' Sort: (Smallest to Largest) Phys Sample Insp. Method Insp Counter  Autofit Contents, Align Left
'
'
    ActiveWorkbook.Worksheets("[COLOR=#ff0000]CondensedFile2InspLots[/COLOR]").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("[COLOR=#ff0000]CondensedFile2InspLots[/COLOR]").Sort.SortFields.Add Key:= _
        Range("N2:N69"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("[COLOR=#ff0000]CondensedFile2InspLots[/COLOR]").Sort.SortFields.Add Key:= _
        Range("E2:E69"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("[COLOR=#ff0000]CondensedFile2InspLots[/COLOR]").Sort.SortFields.Add Key:= _
        Range("BF2:BF69"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("[COLOR=#ff0000]CondensedFile2InspLots[/COLOR]").Sort
        .SetRange Range("A1:BF69")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Cells.Select
    Cells.EntireColumn.AutoFit
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

I would like to take this code and make it applicable to any file I place in there. If you look at the red text I highlighted in the code, it looks like it is taking the file name into account and if I try to run this macro on files with a different name, I get an error message stating:

"Run-time error '9'
Subscript out of range

Debug highlights the following piece of code:

Code:
ActiveWorkbook.Worksheets("CondensedFile2InspLots").Sort.SortFields.Clear

I am using Excel 2007

:biggrin: Thanks for your time! :biggrin:
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Worked perfect! I used your provided statement and plugged it into the code. Here is the amended code. Thank you so much!

Code:
Sub Uzie_2_SortTidyUp()
'
' Uzie_2_SortTidyUp Macro
'
'
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:= _
Range("N2:N69"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:= _
Range("E2:E69"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:= _
Range("BF2:BF69"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:BF69")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Cells.Select
Cells.EntireColumn.AutoFit
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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