Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Run macro in one open workbook only

Hi All,

I'm just a noob to VBA so i need some help which may be obvious.

I need to run a macro when save / saveas is used within WorkbookX. got
that part worked out,

but it tries to run in other workbooks that are also open, which I
don't want it to, because it is referring to sheets that only exist in
WorkbookX. Here's the code from WorkbookX

Sub Workbook_Open()

Application.CommandBars("Standard").Controls("&Sav e").OnAction =
"MySave"

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save").OnAction = "MySave"

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save As...").OnAction = "MySaveAs"

End Sub

Sub Workbook_BeforeClose(Cancel As Boolean)

Application.CommandBars("Standard").Controls("&Sav e").OnAction = ""

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save").OnAction = ""

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save As...").OnAction = ""

End Sub


So how do I limit this working to just WorkbookX - while still allowing
the user to change WorkbookX's name using SaveAs?

thanks

Paul

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Run macro in one open workbook only

Why not just add your macro to the save event in that workbook

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
'your macro
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Paul S" wrote in message
oups.com...
Hi All,

I'm just a noob to VBA so i need some help which may be obvious.

I need to run a macro when save / saveas is used within WorkbookX. got
that part worked out,

but it tries to run in other workbooks that are also open, which I
don't want it to, because it is referring to sheets that only exist in
WorkbookX. Here's the code from WorkbookX

Sub Workbook_Open()

Application.CommandBars("Standard").Controls("&Sav e").OnAction =
"MySave"

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save").OnAction = "MySave"

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save As...").OnAction = "MySaveAs"

End Sub

Sub Workbook_BeforeClose(Cancel As Boolean)

Application.CommandBars("Standard").Controls("&Sav e").OnAction = ""

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save").OnAction = ""

Application.CommandBars("Worksheet Menu
Bar").Controls("File").Controls("Save As...").OnAction = ""

End Sub


So how do I limit this working to just WorkbookX - while still allowing
the user to change WorkbookX's name using SaveAs?

thanks

Paul



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Run macro in one open workbook only

Thanks Bob,

I should have posted my macro as well, it also contains a save, so I'm
not sure if this way will work, all my sheets except one need to be
hidden in any saved file (one way to secure it a little better)

Here is my macro

Sub MySave()

Application.ScreenUpdating = False

ReturnSheet = ActiveSheet.Name
ReturnAddress = ActiveCell.Address

'hide all the sheets except one
For i = 2 To Sheets.Count
Sheets(i).Visible = xlVeryHidden
Next i

ActiveWorkbook.Save

'unhide all the sheets again
Show_all

Sheets(ReturnSheet).Select
Range(ReturnAddress).Select

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Run macro in one open workbook only

Paul,

You could put all that code in the BeforeClose event of your one workbook,
remove it from elsewhere.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Paul S" wrote in message
ups.com...
Thanks Bob,

I should have posted my macro as well, it also contains a save, so I'm
not sure if this way will work, all my sheets except one need to be
hidden in any saved file (one way to secure it a little better)

Here is my macro

Sub MySave()

Application.ScreenUpdating = False

ReturnSheet = ActiveSheet.Name
ReturnAddress = ActiveCell.Address

'hide all the sheets except one
For i = 2 To Sheets.Count
Sheets(i).Visible = xlVeryHidden
Next i

ActiveWorkbook.Save

'unhide all the sheets again
Show_all

Sheets(ReturnSheet).Select
Range(ReturnAddress).Select

End Sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Run macro in one open workbook only

Ok thanks Bob,

have done that mostly into beforesave,

but it is still spitting out errors when another workbook B is open and
I try to Quit Excel (using red cross or FileExit) from Workbook B
window when my workboook A is also open.

If I use the small black cross - or FileClose on each workbook, no
troubles at all.

It seems that it is trying to look for sheets in Workbook B that only
exist in Workbook A, and subsequently can't find the ranges I am
looking for.

any ideas on making the macros in the beforesave event, only apply to
Workbook A, so that when quitting Excel from Workbook B, it doesn't
interfere with the macros in Workbook A

Hope this makes sense

cheers
Paul



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Run macro in one open workbook only

or check somehow that if it is running in Workbook A, run the macro,
and if in another workbook, skip the macro and just do a normal
save/saveas

cheers

Paul

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
Macro in 2nd Open workbook Steve Excel Discussion (Misc queries) 1 January 28th 08 05:35 PM
run macro on workbook open kevin Excel Discussion (Misc queries) 3 March 4th 05 10:12 AM
how can I run a macro when I open a workbook? filo666 Excel Discussion (Misc queries) 2 March 2nd 05 07:56 PM
Macro to open another workbook missmelis01[_2_] Excel Programming 1 August 27th 04 08:37 PM
Open & run a workbook macro. Julian Milano[_2_] Excel Programming 7 January 29th 04 12:40 AM


All times are GMT +1. The time now is 10:44 PM.

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"