Running Sub on only selected sheets

Monaco

New Member
Joined
Nov 29, 2016
Messages
11
Now I have good code that searches through each sheet in the workbook and runs a routine, but I have added some sheets where this sub is not necessary to have run there. How can I specify the specific sheets I want this to run in? (They are named sheets like "EG-P-GIJ - S").

Sub CheckBalances()
Dim ws As Worksheet
Dim lngRow As Long
Dim lngCount As Long
Dim FormulaRange As Range
Dim NotSentMsg As String
Dim MyMsg As String
Dim SentMsg As String
Dim MyLimit As Double
Dim Sheet As String
Dim current As Variant

For Each current In Worksheets
MsgBox current.Name
If current.Range("C80").Value <> 0 Then
current.Activate
Mail_small_Text_Outlook
MsgBox current.Name
End If
Next
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

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,711
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
You can set up an array of the sheets that you want to work with like this:
Code:
Dim ws As Worksheet
For Each ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4"))
    MsgBox ws.Name
    If ws.Range("C80").Value <> 0 Then
        ws.Activate
        Mail_small_Text_Outlook
        MsgBox ws.Name
    End If
Next ws
Just insert your sheet names.
 
Last edited:
Upvote 0

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
There are several approaches to do what you want. You can use a list for a lot of sheets you want to search and refer to the cells one at a time in a loop to do the the search. Or, if you only have two or three additional sheets which you do not want to search you can exclude them in the code like:
Code:
Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
 If sh.Name <> "Sheet1" And sh.Name <> "Sheet2" And sh.Name <> "Sheet3" Then
  'Do something
 End If
Next
 
Upvote 0

Monaco

New Member
Joined
Nov 29, 2016
Messages
11
I have that in there now and have removed "For Each current In Worksheets"

When it runs it stops at MsgBox current.Name and says "Object Required"

Do you know the cause of that?
 
Upvote 0

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
I have that in there now and have removed "For Each current In Worksheets"

When it runs it stops at MsgBox current.Name and says "Object Required"

Do you know the cause of that?

Please post the code that throws the error.
 
Upvote 0

Forum statistics

Threads
1,191,229
Messages
5,985,406
Members
439,962
Latest member
max_york

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
Top