Trouble with Recorded Macro working in multiple worksheets

gbrennan

New Member
Joined
Sep 21, 2011
Messages
1
MS Excel 2010.

I attempted to record a macro to sort data within an active worksheet. I want the Macro to work in the active worksheet of the multiple worsheet workbook. Below is the macro as recorded. When I go to a worksheet where I did not physically record the macro, making it the "active worksheet", and attempt to run the macro, it goes back to the worksheet where I recorded the Macro and runs it only on that sheet (which does no good because my data is already sorted on that sheet from the last time that I ran the Macro). Does any one have a solution?

Sub Datasort()
'
' Datasort Macro
' Sorts Timesheet Data
'
' Keyboard Shortcut: Ctrl+s
'
ActiveCell.Offset(-19, -4).Range("A1:H34").Select
ActiveWorkbook.Worksheets("Tue 092011").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Tue 092011").Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 4).Range("A1:A34"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Tue 092011").Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 5).Range("A1:A34"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Tue 092011").Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 7).Range("A1:A34"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Tue 092011").Sort
.SetRange ActiveCell.Offset(-1, 0).Range("A1:H35")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveCell.Offset(15, 0).Range("A1").Select
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this

Code:
Sub Datasort()
'
' Datasort Macro
' Sorts Timesheet Data
'
' Keyboard Shortcut: Ctrl+s
'
ActiveCell.Offset(-19, -4).Range("A1:H34").Select
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 4).Range("A1:A34"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 5).Range("A1:A34"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 7).Range("A1:A34"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveSheet.Sort
    .SetRange ActiveCell.Offset(-1, 0).Range("A1:H35")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveCell.Offset(15, 0).Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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