Finding worksheet out of open workbooks

ks1987

New Member
Joined
Aug 8, 2012
Messages
24
Hi All,

I am trying to write a code which accomplishes the following:

1. Amongst all open workbook activate workbook with 10th sheet named as Run
2. Display message if no such sheet found in any workbook

I have written the following code for the first point:

Code:
Sub IdentifyValidationTool()
Dim w As Workbook

For Each w In Workbooks
If w.Worksheets(10).Name = "Run" Then
w.Activate
End If
Next w
End Sub

I am struggling to figure out the logic and the code for the second part. Any help on the same is much appreciated
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Perhaps

Code:
Sub IdentifyValidationTool()
Dim w As Workbook
Dim Found As Boolean
For Each w In Workbooks
    If w.Worksheets(10).Name = "Run" Then
        Found = True
        w.Activate
        Exit For
    End If
Next w
If Not Found Then MsgBox "Not found"
End Sub
 
Upvote 0
Perhaps

Code:
Sub IdentifyValidationTool()
Dim w As Workbook
Dim Found As Boolean
For Each w In Workbooks
    If w.Worksheets(10).Name = "Run" Then
        Found = True
        w.Activate
        Exit For
    End If
Next w
If Not Found Then MsgBox "Not found"
End Sub

Hi VoG,

While testing the first If condition, I encounter the subscript out of range error in case of workbooks where the 10th sheet does not exist. How can I take care of this?

Thanks,
ks1987
 
Upvote 0
Maybe

Code:
Sub IdentifyValidationTool()
Dim w As Workbook
Dim Found As Boolean
For Each w In Workbooks
    If w.Worksheets.Count >= 10 Then
        If w.Worksheets(10).Name = "Run" Then
            Found = True
            w.Activate
            Exit For
        End If
    End If
Next w
If Not Found Then MsgBox "Not found"
End Sub
 
Upvote 0
Maybe

Code:
Sub IdentifyValidationTool()
Dim w As Workbook
Dim Found As Boolean
For Each w In Workbooks
    If w.Worksheets.Count >= 10 Then
        If w.Worksheets(10).Name = "Run" Then
            Found = True
            w.Activate
            Exit For
        End If
    End If
Next w
If Not Found Then MsgBox "Not found"
End Sub

Works like a charm. Great use of Boolean. I look forward to reading up more on it.
 
Upvote 0

Forum statistics

Threads
1,216,750
Messages
6,132,498
Members
449,730
Latest member
SeanHT

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