Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default run code on opening workbook and apply code to certain sheets

I have vba code that I want to run when I open the workbook and I only want
it to apply to sheet3, sheet5 and sheet6. Would someone know how I need to
write this? Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default run code on opening workbook and apply code to certain sheets

Something like

Private Sub Workbook_Open()
Mymacro Worksheets("Sheet3")
Mymacro Worksheets("Sheet5")

Mymacro Worksheets("Sheet6")
End Sub

where your macro would be able to take a worksheet object as a parameter and
process that object.

'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)


"Jane" wrote in message
...
I have vba code that I want to run when I open the workbook and I only

want
it to apply to sheet3, sheet5 and sheet6. Would someone know how I need to
write this? Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default run code on opening workbook and apply code to certain sheets

Thank you very much. Do I need to put my code with the code you gave me and
make it all as one? and what does "mymacro" represent? Forgive me I am
still learning. Thanks.

"Bob Phillips" wrote:

Something like

Private Sub Workbook_Open()
Mymacro Worksheets("Sheet3")
Mymacro Worksheets("Sheet5")

Mymacro Worksheets("Sheet6")
End Sub

where your macro would be able to take a worksheet object as a parameter and
process that object.

'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)


"Jane" wrote in message
...
I have vba code that I want to run when I open the workbook and I only

want
it to apply to sheet3, sheet5 and sheet6. Would someone know how I need to
write this? Thanks.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default run code on opening workbook and apply code to certain sheets

mymacro is the macro for your VBA code.

Your code can be in standard code module, it doesn't have to go with this
code. The important thing is to ensue your code can handle variable
worksheet objects.

--

HTH

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


"Jane" wrote in message
...
Thank you very much. Do I need to put my code with the code you gave me

and
make it all as one? and what does "mymacro" represent? Forgive me I am
still learning. Thanks.

"Bob Phillips" wrote:

Something like

Private Sub Workbook_Open()
Mymacro Worksheets("Sheet3")
Mymacro Worksheets("Sheet5")

Mymacro Worksheets("Sheet6")
End Sub

where your macro would be able to take a worksheet object as a parameter

and
process that object.

'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)


"Jane" wrote in message
...
I have vba code that I want to run when I open the workbook and I only

want
it to apply to sheet3, sheet5 and sheet6. Would someone know how I

need to
write this? Thanks.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default run code on opening workbook and apply code to certain sheets

Thank you again and even more for helping me on Sunday. My code currently
says: ThisWorkbook.Worksheets("sheet3").Activate so I have the code
currently saved under that one spreadsheet while I was testing it. Is that
wrong? Does it need to go somewhere else? I need to put it "somewhere" so
it knows to run on certain spreadsheets. I ran your code and changed
"myMacro" to "colorformat" which is my "sub" name but the code stopped right
after: Private Sub Workbook_Open(). Is that wrong?

"Bob Phillips" wrote:

mymacro is the macro for your VBA code.

Your code can be in standard code module, it doesn't have to go with this
code. The important thing is to ensue your code can handle variable
worksheet objects.

--

HTH

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


"Jane" wrote in message
...
Thank you very much. Do I need to put my code with the code you gave me

and
make it all as one? and what does "mymacro" represent? Forgive me I am
still learning. Thanks.

"Bob Phillips" wrote:

Something like

Private Sub Workbook_Open()
Mymacro Worksheets("Sheet3")
Mymacro Worksheets("Sheet5")

Mymacro Worksheets("Sheet6")
End Sub

where your macro would be able to take a worksheet object as a parameter

and
process that object.

'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)


"Jane" wrote in message
...
I have vba code that I want to run when I open the workbook and I only
want
it to apply to sheet3, sheet5 and sheet6. Would someone know how I

need to
write this? Thanks.








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default run code on opening workbook and apply code to certain sheets

Your macro should now look something like this

Sub colorformat(sh As Worksheet)
sh.Activate
End Sub

This is code that should be placed in a standard code module (InsertModule)
in the VBE, not within the worksheet code module.

The other code should go in the ThisWorkbook code module as I mentioned. To
be doubly sure you could change it to

Private Sub Workbook_Open()
colorformat ThisWorkbook.Worksheets("Sheet3")
colorformat ThisWorkbook.Worksheets("Sheet5")
colorformat ThisWorkbook.Worksheets("Sheet6")
End Sub

although that shouldn't be absolutely necessary as it will be the active
workbook by virtue of being opened, but it does no harm.

I hope your colorformat macro will do more later, as activating one sheet
after another doesn't have a lot of point :-).

--

HTH

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


"Jane" wrote in message
...
Thank you again and even more for helping me on Sunday. My code currently
says: ThisWorkbook.Worksheets("sheet3").Activate so I have the code
currently saved under that one spreadsheet while I was testing it. Is

that
wrong? Does it need to go somewhere else? I need to put it "somewhere"

so
it knows to run on certain spreadsheets. I ran your code and changed
"myMacro" to "colorformat" which is my "sub" name but the code stopped

right
after: Private Sub Workbook_Open(). Is that wrong?

"Bob Phillips" wrote:

mymacro is the macro for your VBA code.

Your code can be in standard code module, it doesn't have to go with

this
code. The important thing is to ensue your code can handle variable
worksheet objects.

--

HTH

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


"Jane" wrote in message
...
Thank you very much. Do I need to put my code with the code you gave

me
and
make it all as one? and what does "mymacro" represent? Forgive me I

am
still learning. Thanks.

"Bob Phillips" wrote:

Something like

Private Sub Workbook_Open()
Mymacro Worksheets("Sheet3")
Mymacro Worksheets("Sheet5")

Mymacro Worksheets("Sheet6")
End Sub

where your macro would be able to take a worksheet object as a

parameter
and
process that object.

'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)


"Jane" wrote in message
...
I have vba code that I want to run when I open the workbook and I

only
want
it to apply to sheet3, sheet5 and sheet6. Would someone know how I

need to
write this? 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
Block cracking code from opening protected excel sheets Barbara Excel Discussion (Misc queries) 5 March 31st 10 05:00 PM
Code for opening a workbook at a specific time!?! mike_vr Excel Discussion (Misc queries) 2 June 5th 07 04:55 PM
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Excel Worksheet Functions 6 March 29th 06 12:43 PM
Performing code without opening the Workbook Marishah Warren Excel Programming 2 December 25th 03 10:04 AM
Code for Opening a Closed Workbook scrabtree23[_2_] Excel Programming 3 November 11th 03 03:48 PM


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