I am very new to writing/recording macros. I have code that works for sorting data in a worksheet. I recorded a macro, and then copied the code into my other macro in VBA. However, I want to be able to run the macro on any worksheet. When I recorded the macro, it identifies the specific active worksheet I was working in.
How do I change the following code so that I can use this macro on any worksheet that I open?
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Region 2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Region 2").Sort.SortFields.Add Key:=Range( _
"J3:J9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Region 2").Sort
.SetRange Range("A3:M4783")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I also want to change the range I sort to capture however many lines of data the new worksheet may have. It may have 4000 rows, it may have 6000 rows. How do I change that?
Thank you!
Bookmarks