Macro only works on initial worksheet

AvoidingVBA

New Member
Joined
Dec 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I'm working on a template that allows me to repeatedly copy the initial worksheet (e.g. sheet 1) and have multiple worksheets in a single file. I do repetitive data entry/manipulation and a system like this is key. I have a variety of macros I use for this workbook, some assigned to buttons and others live in the ribbon. All of these are recorded since I'm unfamiliar with VBA.

I have a macro that creates a custom sort list for my data, since copying a worksheet doesn't also copy custom sort/filter settings. The problem is when I run the macro on any worksheet except the initial one (e.g. sheet 1(1)), it still sorts on the first worksheet, not the current worksheet. Does anyone know what causes this? Is there a step I can take in recording to prevent this?

Thanks
 

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
It would help if you could share your macro code. It is likely a simple thing like using 'activesheet' vs a specific sheet.
 
Upvote 0
It would help if you could share your macro code. It is likely a simple thing like using 'activesheet' vs a specific sheet.
Yes, I'm unsure how to change it from activesheet when recording. Here's the code:
VBA Code:
Sub Live_Sort()
'
' Live_Sort Macro
'

'
    ActiveWorkbook.Worksheets("Copy Sheet").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Copy Sheet").Sort.SortFields.Add2 Key:=Range( _
        "A11:A54"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "Equity,Allocation,Unknown,BDC,Fixed,Cash/Equiv", DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Copy Sheet").Sort.SortFields.Add(Range("B11:B54"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
        , 240)
    ActiveWorkbook.Worksheets("Copy Sheet").Sort.SortFields.Add(Range("B11:B54"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(231, _
        230, 230)
    ActiveWorkbook.Worksheets("Copy Sheet").Sort.SortFields.Add2 Key:=Range( _
        "B11:B54"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Copy Sheet").Sort
        .SetRange Range("A10:J54")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited by a moderator:
Upvote 0
Give this a try:
change instances of:
ActiveWorkbook.Worksheets("Copy Sheet").
to:
ActiveWorkbook.ActiveSheet.
 
Upvote 0
Solution
How about
VBA Code:
Sub Live_Sort()
'
' Live_Sort Macro
'

'
   With ActiveSheet.Sort
      .SortFields.Clear
      .SortFields.Add2 Key:=Range( _
         "A11:A54"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
         "Equity,Allocation,Unknown,BDC,Fixed,Cash/Equiv", DataOption:=xlSortNormal
      .SortFields.Add(Range("B11:B54"), _
         xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
         , 240)
      .SortFields.Add(Range("B11:B54"), _
         xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(231, _
         230, 230)
      .SortFields.Add2 Key:=Range( _
         "B11:B54"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
         xlSortTextAsNumbers
    
       .SetRange Range("A10:J54")
       .Header = xlYes
       .MatchCase = False
       .Orientation = xlTopToBottom
       .SortMethod = xlPinYin
       .Apply
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Live_Sort()
'
' Live_Sort Macro
'

'
   With ActiveSheet.Sort
      .SortFields.Clear
      .SortFields.Add2 Key:=Range( _
         "A11:A54"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
         "Equity,Allocation,Unknown,BDC,Fixed,Cash/Equiv", DataOption:=xlSortNormal
      .SortFields.Add(Range("B11:B54"), _
         xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
         , 240)
      .SortFields.Add(Range("B11:B54"), _
         xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(231, _
         230, 230)
      .SortFields.Add2 Key:=Range( _
         "B11:B54"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
         xlSortTextAsNumbers
   
       .SetRange Range("A10:J54")
       .Header = xlYes
       .MatchCase = False
       .Orientation = xlTopToBottom
       .SortMethod = xlPinYin
       .Apply
   End With
End Sub
Nice and clean :)
 
Upvote 0
How about
VBA Code:
Sub Live_Sort()
'
' Live_Sort Macro
'

'
   With ActiveSheet.Sort
      .SortFields.Clear
      .SortFields.Add2 Key:=Range( _
         "A11:A54"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
         "Equity,Allocation,Unknown,BDC,Fixed,Cash/Equiv", DataOption:=xlSortNormal
      .SortFields.Add(Range("B11:B54"), _
         xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
         , 240)
      .SortFields.Add(Range("B11:B54"), _
         xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(231, _
         230, 230)
      .SortFields.Add2 Key:=Range( _
         "B11:B54"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
         xlSortTextAsNumbers
   
       .SetRange Range("A10:J54")
       .Header = xlYes
       .MatchCase = False
       .Orientation = xlTopToBottom
       .SortMethod = xlPinYin
       .Apply
   End With
End Sub
This worked perfectly. Thank you!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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