Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
peter147
 
Posts: n/a
Default How do I build a Macro that can identify wooksheet names

I am building a macro within excel, I have to switch between a number of
worksheets within a workbook. I need the same macro to run in a number of
similar workbooks; however some workbooks do not contain all the worksheets.
How can I use a formula to ignore the commands relating to the missing
worksheets?
Thank you for your help

Peter Davenport




  #2   Report Post  
Robin Hammond
 
Posts: n/a
Default

Peter,

Try this:

Sub MainLoop
Dim vSheets as Variant
Dim vSheet as Variant
vSheets = Array("A","B","C")
For Each vSheet in vSheets
If SheetExists(vSheet) Then
'do something
End If
Next vSheet
End Sub

Public Function SheetExists(strName As String, Optional wbName As Workbook)
As Boolean
Dim shTest As Worksheet
If ActiveWorkbook Is Nothing And wbName Is Nothing Then Exit Function
If wbName Is Nothing Then Set wbName = ActiveWorkbook
On Error Resume Next
Set shTest = wbName.Sheets(strName)
On Error GoTo 0
SheetExists = Not shTest Is Nothing
End Function

Robin Hammond
www.enhanceddatasystems.com

"peter147" wrote in message
...
I am building a macro within excel, I have to switch between a number of
worksheets within a workbook. I need the same macro to run in a number of
similar workbooks; however some workbooks do not contain all the
worksheets.
How can I use a formula to ignore the commands relating to the missing
worksheets?
Thank you for your help

Peter Davenport






  #3   Report Post  
Jim May
 
Posts: n/a
Default

With Dim vSheet as "Variant"
within the Function SheetExists can I use strName As "String"?


"Robin Hammond" wrote in message
...
Peter,

Try this:

Sub MainLoop
Dim vSheets as Variant
Dim vSheet as Variant
vSheets = Array("A","B","C")
For Each vSheet in vSheets
If SheetExists(vSheet) Then
'do something
End If
Next vSheet
End Sub

Public Function SheetExists(strName As String, Optional wbName As

Workbook)
As Boolean
Dim shTest As Worksheet
If ActiveWorkbook Is Nothing And wbName Is Nothing Then Exit Function
If wbName Is Nothing Then Set wbName = ActiveWorkbook
On Error Resume Next
Set shTest = wbName.Sheets(strName)
On Error GoTo 0
SheetExists = Not shTest Is Nothing
End Function

Robin Hammond
www.enhanceddatasystems.com

"peter147" wrote in message
...
I am building a macro within excel, I have to switch between a number of
worksheets within a workbook. I need the same macro to run in a number

of
similar workbooks; however some workbooks do not contain all the
worksheets.
How can I use a formula to ignore the commands relating to the missing
worksheets?
Thank you for your help

Peter Davenport








  #4   Report Post  
Robin Hammond
 
Posts: n/a
Default

Oops. The function's been tested. I just wrote the MainLoop on the fly in
the message to illustrate. It should have a type conversion to string in the
call to the function as follows.

Sub MainLoop()
Dim vSheets As Variant
Dim vSheet As Variant
vSheets = Array("A", "B", "C")
For Each vSheet In vSheets
If SheetExists(CStr(vSheet)) Then
'do something
End If
Next vSheet
End Sub

Public Function SheetExists(strName As String, Optional wbName As Workbook)
As Boolean
Dim shTest As Worksheet
If ActiveWorkbook Is Nothing And wbName Is Nothing Then Exit Function
If wbName Is Nothing Then Set wbName = ActiveWorkbook
On Error Resume Next
Set shTest = wbName.Sheets(strName)
On Error GoTo 0
SheetExists = Not shTest Is Nothing
End Function

Robin Hammond
www.enhanceddatasystems.com

"Jim May" wrote in message
news:ZxEje.13791$Fv.8377@lakeread01...
With Dim vSheet as "Variant"
within the Function SheetExists can I use strName As "String"?


"Robin Hammond" wrote in message
...
Peter,

Try this:

Sub MainLoop
Dim vSheets as Variant
Dim vSheet as Variant
vSheets = Array("A","B","C")
For Each vSheet in vSheets
If SheetExists(vSheet) Then
'do something
End If
Next vSheet
End Sub

Public Function SheetExists(strName As String, Optional wbName As

Workbook)
As Boolean
Dim shTest As Worksheet
If ActiveWorkbook Is Nothing And wbName Is Nothing Then Exit Function
If wbName Is Nothing Then Set wbName = ActiveWorkbook
On Error Resume Next
Set shTest = wbName.Sheets(strName)
On Error GoTo 0
SheetExists = Not shTest Is Nothing
End Function

Robin Hammond
www.enhanceddatasystems.com

"peter147" wrote in message
...
I am building a macro within excel, I have to switch between a number of
worksheets within a workbook. I need the same macro to run in a number

of
similar workbooks; however some workbooks do not contain all the
worksheets.
How can I use a formula to ignore the commands relating to the missing
worksheets?
Thank you for your help

Peter Davenport










  #5   Report Post  
Jim May
 
Posts: n/a
Default

Thanks Robin;
I think I actually understand,,
WOW !!
Appreciate your help,
Jim

"Robin Hammond" wrote in message
...
Oops. The function's been tested. I just wrote the MainLoop on the fly in
the message to illustrate. It should have a type conversion to string in

the
call to the function as follows.

Sub MainLoop()
Dim vSheets As Variant
Dim vSheet As Variant
vSheets = Array("A", "B", "C")
For Each vSheet In vSheets
If SheetExists(CStr(vSheet)) Then
'do something
End If
Next vSheet
End Sub

Public Function SheetExists(strName As String, Optional wbName As

Workbook)
As Boolean
Dim shTest As Worksheet
If ActiveWorkbook Is Nothing And wbName Is Nothing Then Exit Function
If wbName Is Nothing Then Set wbName = ActiveWorkbook
On Error Resume Next
Set shTest = wbName.Sheets(strName)
On Error GoTo 0
SheetExists = Not shTest Is Nothing
End Function

Robin Hammond
www.enhanceddatasystems.com

"Jim May" wrote in message
news:ZxEje.13791$Fv.8377@lakeread01...
With Dim vSheet as "Variant"
within the Function SheetExists can I use strName As "String"?


"Robin Hammond" wrote in message
...
Peter,

Try this:

Sub MainLoop
Dim vSheets as Variant
Dim vSheet as Variant
vSheets = Array("A","B","C")
For Each vSheet in vSheets
If SheetExists(vSheet) Then
'do something
End If
Next vSheet
End Sub

Public Function SheetExists(strName As String, Optional wbName As

Workbook)
As Boolean
Dim shTest As Worksheet
If ActiveWorkbook Is Nothing And wbName Is Nothing Then Exit Function
If wbName Is Nothing Then Set wbName = ActiveWorkbook
On Error Resume Next
Set shTest = wbName.Sheets(strName)
On Error GoTo 0
SheetExists = Not shTest Is Nothing
End Function

Robin Hammond
www.enhanceddatasystems.com

"peter147" wrote in message
...
I am building a macro within excel, I have to switch between a number

of
worksheets within a workbook. I need the same macro to run in a

number
of
similar workbooks; however some workbooks do not contain all the
worksheets.
How can I use a formula to ignore the commands relating to the

missing
worksheets?
Thank you for your help

Peter Davenport












Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issuing macro in workbook from separate workbook Nigel Excel Discussion (Misc queries) 1 May 16th 05 05:46 PM
Can I build 'goal seek' facility into a macro? sprasanna Excel Discussion (Misc queries) 1 April 28th 05 08:07 AM
Playing a macro from another workbook Jim Excel Discussion (Misc queries) 1 February 23rd 05 10:12 PM
Date macro Hiking Excel Discussion (Misc queries) 9 February 3rd 05 12:40 AM
Macro and If Statement SATB Excel Discussion (Misc queries) 2 December 3rd 04 04:46 PM


All times are GMT +1. The time now is 05:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"