Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 199
Default Macro for active sheet only

I have a macro which applies to, and is recorded within, one specific
spreadsheet, says, stats.xls. I have linked it to a custom button and it
works well.

However, I usually have several spreadsheets open simultaneously and
occasionally I inadvertently run hit the button and run the macro even when
I am working on one of the other sheets. This is not disastrous but is there
a way I can add some code to the macro so that it will only run when
stats.xls is the active sheet 'at the front'.

Thanks,

V

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Macro for active sheet only

You could check something on that sheet (if it's nice and unique):

Option Explicit
Sub YourMacro()
if activesheet.range("A1").value < "this is the correct Sheet" then
exit sub
end if

'rest of code here
End sub

You could look for a nice unique header (a couple of cells???). Or a comment in
a cell--or a name or anything you can think of.

But my bet is that some day, you may want to run it against a worksheet where
you won't find that key info.

I'd just ask...

Option Explicit
Sub YourMacro()

dim Resp as long

resp = msgbox(Prompt:="You're about to run the macro that ...", _
buttons:=vbokcancel)

if resp = vbcancel then
exit sub
end if

'rest of code here
End sub

Victor Delta wrote:

I have a macro which applies to, and is recorded within, one specific
spreadsheet, says, stats.xls. I have linked it to a custom button and it
works well.

However, I usually have several spreadsheets open simultaneously and
occasionally I inadvertently run hit the button and run the macro even when
I am working on one of the other sheets. This is not disastrous but is there
a way I can add some code to the macro so that it will only run when
stats.xls is the active sheet 'at the front'.

Thanks,

V


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 22,906
Default Macro for active sheet only

You keep referring to stats.xls as a "sheet"

stats.xls is workbook, not a worksheet.

In your code, point the macro to Thisworkbook and whatever sheet in the
workbook you want to run the macro upon.


Gord Dibben MS Excel MVP



On Tue, 20 Oct 2009 23:31:06 +0100, "Victor Delta" wrote:

I have a macro which applies to, and is recorded within, one specific
spreadsheet, says, stats.xls. I have linked it to a custom button and it
works well.

However, I usually have several spreadsheets open simultaneously and
occasionally I inadvertently run hit the button and run the macro even when
I am working on one of the other sheets. This is not disastrous but is there
a way I can add some code to the macro so that it will only run when
stats.xls is the active sheet 'at the front'.

Thanks,

V


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 199
Default Macro for active sheet only

Dave

Many thanks. I'll try a few experiments based on your suggestions.

V

"Dave Peterson" wrote in message
...
You could check something on that sheet (if it's nice and unique):

Option Explicit
Sub YourMacro()
if activesheet.range("A1").value < "this is the correct Sheet" then
exit sub
end if

'rest of code here
End sub

You could look for a nice unique header (a couple of cells???). Or a
comment in
a cell--or a name or anything you can think of.

But my bet is that some day, you may want to run it against a worksheet
where
you won't find that key info.

I'd just ask...

Option Explicit
Sub YourMacro()

dim Resp as long

resp = msgbox(Prompt:="You're about to run the macro that ...", _
buttons:=vbokcancel)

if resp = vbcancel then
exit sub
end if

'rest of code here
End sub

Victor Delta wrote:

I have a macro which applies to, and is recorded within, one specific
spreadsheet, says, stats.xls. I have linked it to a custom button and it
works well.

However, I usually have several spreadsheets open simultaneously and
occasionally I inadvertently run hit the button and run the macro even
when
I am working on one of the other sheets. This is not disastrous but is
there
a way I can add some code to the macro so that it will only run when
stats.xls is the active sheet 'at the front'.

Thanks,

V


--

Dave Peterson


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 199
Default Macro for active sheet only

Gord

Thanks - absolutely right. I'm afraid my question used some sloppy terms.

V

"Gord Dibben" <gorddibbATshawDOTca wrote in message
...
You keep referring to stats.xls as a "sheet"

stats.xls is workbook, not a worksheet.

In your code, point the macro to Thisworkbook and whatever sheet in the
workbook you want to run the macro upon.


Gord Dibben MS Excel MVP



On Tue, 20 Oct 2009 23:31:06 +0100, "Victor Delta"
wrote:

I have a macro which applies to, and is recorded within, one specific
spreadsheet, says, stats.xls. I have linked it to a custom button and it
works well.

However, I usually have several spreadsheets open simultaneously and
occasionally I inadvertently run hit the button and run the macro even
when
I am working on one of the other sheets. This is not disastrous but is
there
a way I can add some code to the macro so that it will only run when
stats.xls is the active sheet 'at the front'.

Thanks,

V



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
diable macro if a certain sheet is active belvy123 Excel Discussion (Misc queries) 3 July 7th 09 08:39 AM
emailing active sheet with macro BIGBOY1974AA Excel Worksheet Functions 2 July 18th 08 08:23 AM
macro active sheet liem Excel Discussion (Misc queries) 1 January 18th 08 05:48 AM
copy name from active sheet to cell - using macro or function dymek Excel Worksheet Functions 2 October 2nd 06 12:32 PM
How to change macro so it performs actions on ACTIVE sheet? Tom9283 Excel Discussion (Misc queries) 6 April 15th 05 07:32 AM


All times are GMT +1. The time now is 04:29 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"