Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default Run macro automatically upon opening or closing document


How do I automatically run this macro when the workseet is either opened or
closed?


Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run macro automatically upon opening or closing document


Munchkin;373278 Wrote:
How do I automatically run this macro when the workseet is either opened
or
closed?


Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End SubIf you really want to run the same macro on open and close then put the

code below in the ThisWorkbook module, to be honest you select objects
when you don't need to and they perform no action, you will probably
want error handling too!

Code:
--------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End Sub


Private Sub Workbook_Open()
Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End Sub
--------------------


--
Simon Lloyd

Regards,
Simon Lloyd
'The Code Cage' (http://www.thecodecage.com)
------------------------------------------------------------------------
Simon Lloyd's Profile: http://www.thecodecage.com/forumz/member.php?userid=1
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=104481

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Run macro automatically upon opening or closing document

You have a problem with your on close. The changes are going to require the
file to be saved. That being said since the code is being run on open the on
close is not necessary. I would be inclined to just do this...

Private Sub Workbook_Open()
With Sheets("Sheet1") 'Change to the appropriate sheet name
.Rows("2:3").EntireRow.Hidden = True
.Rows("6:7").EntireRow.Hidden = True
.select
.Range("C12").Select
End with
End Sub

Right click the XL icon in the upper left corner of the Excel window and
select View Code. This takes you into the ThisWorkbook module. Post the code
above.

--
HTH...

Jim Thomlinson


"Simon Lloyd" wrote:


Munchkin;373278 Wrote:
How do I automatically run this macro when the workseet is either opened
or
closed?


Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End SubIf you really want to run the same macro on open and close then put the

code below in the ThisWorkbook module, to be honest you select objects
when you don't need to and they perform no action, you will probably
want error handling too!

Code:
--------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End Sub


Private Sub Workbook_Open()
Rows("2:3").Select
Range("B2").Activate
Selection.EntireRow.Hidden = True
Rows("6:7").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("C12").Select
End Sub
--------------------


--
Simon Lloyd

Regards,
Simon Lloyd
'The Code Cage' (http://www.thecodecage.com)
------------------------------------------------------------------------
Simon Lloyd's Profile: http://www.thecodecage.com/forumz/member.php?userid=1
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=104481


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
Set macro to run automatically when closing workbook? Wuddus Excel Discussion (Misc queries) 10 January 20th 15 03:39 PM
Updating Document Without Closing and Re-Opening Randy Excel Discussion (Misc queries) 2 March 4th 09 05:01 PM
How to automatically number a new document(Invoice) when opening Gilly Excel Discussion (Misc queries) 1 September 13th 05 03:46 PM
How to automatically number a new document(Invoice) when opening Gilly Excel Discussion (Misc queries) 0 September 13th 05 02:45 PM
Start Excel Macro upon opening of document Brett Excel Programming 4 June 13th 05 06:27 PM


All times are GMT +1. The time now is 03:01 AM.

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"