ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looking for a sheet (https://www.excelbanter.com/excel-programming/314385-looking-sheet.html)

Dr.Schwartz

Looking for a sheet
 
I want to look though all open workbooks to see if a sheet exists. This is my
code so far:

Dim SDf As Workbook, SDws As Worksheet
For Each SDf In Application.Workbooks
Debug.Print SDf.Name
For Each SDws In Worksheets
Debug.Print SDws.Name
If SDws.Name = "ChemDraw1" Then
Sheets("ChemDraw1").Select
MsgBox ("Correct worksheet detected")
Exit For
End If
Next SDws
Next SDf

From the debug.print I can tell that it only looks through the sheets in the
active workbook. Can anybody help me out?

The Doctor

Bob Phillips[_6_]

Looking for a sheet
 
You need to qualify the worksheets collection with the workbook

Dim SDf As Workbook, SDws As Worksheet
For Each SDf In Application.Workbooks
Debug.Print SDf.Name
For Each SDws In SDf.Worksheets
Debug.Print SDws.Name
If SDws.Name = "ChemDraw1" Then
Sheets("ChemDraw1").Select
MsgBox ("Correct worksheet detected")
Exit For
End If
Next SDws
Next SDf


--

HTH

RP

"Dr.Schwartz" wrote in message
...
I want to look though all open workbooks to see if a sheet exists. This is

my
code so far:

Dim SDf As Workbook, SDws As Worksheet
For Each SDf In Application.Workbooks
Debug.Print SDf.Name
For Each SDws In Worksheets
Debug.Print SDws.Name
If SDws.Name = "ChemDraw1" Then
Sheets("ChemDraw1").Select
MsgBox ("Correct worksheet detected")
Exit For
End If
Next SDws
Next SDf

From the debug.print I can tell that it only looks through the sheets in

the
active workbook. Can anybody help me out?

The Doctor





All times are GMT +1. The time now is 07:48 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com