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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
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
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
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
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,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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