Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Macro in add-in runs depending on if sheet exists in workbook

I have a macro in an add-in (located in the XLSTART folder) which is run
from a toolbar.

I want a routine to run if sheet1 of the current workbook is called "Order
List 1"

If Sheet1.Name = "Order List 1" Then
'Run a routine
Else
MsgBox "This program only works on the `Internet Orders & Packing Sheets'
spread sheet"
Exit Sub
End If

This works if you only open 1 workbook at a time, but if you have more than
one open it sometimes defaults to the sheet1 name of the first worksheet
opened.

Thanks



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Macro in add-in runs depending on if sheet exists in workbook

By current workbook, I presume you mean ActiveWorkbook? Is there a reason
that you need to have a specific worksheet name? Why not define another
worksheet code name for the worksheet of interest. To do this, right click
on the worksheet tab to view Code. Select the worksheet of interest and if
the properties aren't displayed, press F4. You should have an option to
change the name there..

Sub Test()
Dim aWB As Workbook
Dim WS As Worksheet
Dim myWS As Worksheet

Set aWB = ActiveWorkbook

For Each WS In aWB.Worksheets
If WS.CodeName = "Orders_PackingSheets" Then
Set myWS = WS
Exit For
End If
Next WS

If Not myWS Is Nothing Then
'Run a routine
Else
MsgBox "This program only works on the `Internet Orders & " & vbNewLine & _
"Packing Sheets' spread sheet"
Exit Sub
End If

End Sub

--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.



"JC" wrote:

I have a macro in an add-in (located in the XLSTART folder) which is run
from a toolbar.

I want a routine to run if sheet1 of the current workbook is called "Order
List 1"

If Sheet1.Name = "Order List 1" Then
'Run a routine
Else
MsgBox "This program only works on the `Internet Orders & Packing Sheets'
spread sheet"
Exit Sub
End If

This works if you only open 1 workbook at a time, but if you have more than
one open it sometimes defaults to the sheet1 name of the first worksheet
opened.

Thanks




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Macro in add-in runs depending on if sheet exists in workbook

That's work fine, thanks for the post


By current workbook, I presume you mean ActiveWorkbook? Is there a reason
that you need to have a specific worksheet name? Why not define another
worksheet code name for the worksheet of interest. To do this, right
click
on the worksheet tab to view Code. Select the worksheet of interest and
if
the properties aren't displayed, press F4. You should have an option to
change the name there..

Sub Test()
Dim aWB As Workbook
Dim WS As Worksheet
Dim myWS As Worksheet

Set aWB = ActiveWorkbook

For Each WS In aWB.Worksheets
If WS.CodeName = "Orders_PackingSheets" Then
Set myWS = WS
Exit For
End If
Next WS

If Not myWS Is Nothing Then
'Run a routine
Else
MsgBox "This program only works on the `Internet Orders & " & vbNewLine &
_
"Packing Sheets' spread sheet"
Exit Sub
End If

End Sub

--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.



"JC" wrote:

I have a macro in an add-in (located in the XLSTART folder) which is run
from a toolbar.

I want a routine to run if sheet1 of the current workbook is called
"Order
List 1"

If Sheet1.Name = "Order List 1" Then
'Run a routine
Else
MsgBox "This program only works on the `Internet Orders & Packing
Sheets'
spread sheet"
Exit Sub
End If

This works if you only open 1 workbook at a time, but if you have more
than
one open it sometimes defaults to the sheet1 name of the first worksheet
opened.

Thanks






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
Why macro runs on the same sheet? Harshad[_2_] Excel Discussion (Misc queries) 1 September 15th 08 09:39 AM
How to tell if sheet exists in closed workbook Jim[_7_] Excel Programming 3 December 23rd 07 10:26 AM
Before macro runs - check workbook name Jan Excel Programming 5 August 2nd 06 02:25 PM
Check if sheet exists in a closed workbook FrigidDigit[_2_] Excel Programming 2 October 25th 05 06:44 AM
Macro runs in source , but not when in Personal Macro Workbook Darin Kramer Excel Programming 1 September 13th 05 04:48 PM


All times are GMT +1. The time now is 12:42 PM.

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

About Us

"It's about Microsoft Excel"