Changing VBA to run code without activating worksheet

rjheibel

New Member
Joined
Mar 8, 2018
Messages
43
Office Version
  1. 365
Platform
  1. Windows
I am trying to clean up my macros, which were mainly written through the recorder function. In doing this most of the actions were done through activating worksheets and performing commands. I have cleaned up most of the macro, but do not know how to correctly run the following without activating the sheet. This statement was written to add a blank column based on the values in column S. Thanks for your help.

VBA Code:
    Sheets("Sub Activities-Step3").Select
    Application.DisplayAlerts = False
    With Range("S1", Range("S" & Rows.Count).End(xlUp))
        .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
        .Offset(2, -1).SpecialCells(xlCellTypeConstants).Offset(, 1).ClearContents
        .Offset(, -1).EntireColumn.Delete
        .EntireColumn.RemoveSubtotal
    End With
    Application.DisplayAlerts = True
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Without having to select the sheet

VBA Code:
Sub Ha()

  Dim Sht As Worksheet

  Set Sht = Sheets("Sub Activities-Step3")
  Application.DisplayAlerts = False
  With Sht
    With .Range(.Range("S1"), .Range("S" & Rows.Count).End(xlUp))
      .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
      .Offset(2, -1).SpecialCells(xlCellTypeConstants).Offset(, 1).ClearContents
      .Offset(, -1).EntireColumn.Delete
      .EntireColumn.RemoveSubtotal
    End With
  End With
  Application.DisplayAlerts = True
    
    
End Sub
 
Upvote 1
Another option that would be just a bit shorter:
VBA Code:
    Application.DisplayAlerts = False
        With Sheets("Sub Activities-Step3")
            With .Range(.Range("S1"), .Range("S" & Rows.Count).End(xlUp))
                .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
                .Offset(2, -1).SpecialCells(xlCellTypeConstants).Offset(, 1).ClearContents
                .Offset(, -1).EntireColumn.Delete
                .EntireColumn.RemoveSubtotal
            End With
        End With
    Application.DisplayAlerts = True
 
Upvote 1
Solution

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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