add auto sort to the begining of a macro

Jerk24

Board Regular
Joined
Oct 10, 2012
Messages
190
I have a macro that will replace a alphanumeric code, with a corrisponding name that is on sheet4. This use to be two macros. now that I have combined them, the macro will only pick up a few lines. what i need is a way to tell the macro to sort everything on column A, and then run the Macro.

Sub uic()
Dim subs
Set subs = Sheet4
Set r = subs.Range("B2:" & subs.Range("C2").End(xlDown).Address)
For i = 1 To r.Rows.Count
Sheet2.Range("M2:" & Sheet1.Range("M2").End(xlDown).Address).Select
Selection.Replace What:=r.Cells(i, 1), Replacement:=r.Cells(i, 2), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
When in doubt, record a macro that does exactly what you want, then read the code. I recorded a macro and sorted the first column and got something like this:
Code:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A2:A6")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,134
Messages
6,129,070
Members
449,485
Latest member
greggy

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