#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Save Macro

I'd like a macro that, prior to saving the current workbook, would position
the cursor in cell A1, and make the first sheet the active one. The
following code does not work. Can anyone help?

Public Sub SaveWorksheet()
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
Range("A1").Select
Next wrksht
Worksheets(1).Select
ActiveWorkbook.Save
End Sub

Sprinks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Save Macro

Use the before save event for the thisworkbook object/module

http://www.cpearson.com/excel/events.htm

Private Sub Workbook_BeforeSave( _
ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sh as Worksheet
ThisWorkbook.Activate
for each sh in thisworkbook.Worksheets
sh.Activate
sh.Range("A1").Select
Next
thisworkbook.worksheets(1).Select
Cancel = True
On Error goto ErrHandler
Application.EnableEvents = False
ThisWorkbook.Save
ErrHandler:
Application.EnableEvents = True
End Sub

This must be in the ThisWorkbook Module.

--
Regards,
Tom Ogilvy

"Sprinks" wrote in message
...
I'd like a macro that, prior to saving the current workbook, would

position
the cursor in cell A1, and make the first sheet the active one. The
following code does not work. Can anyone help?

Public Sub SaveWorksheet()
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
Range("A1").Select
Next wrksht
Worksheets(1).Select
ActiveWorkbook.Save
End Sub

Sprinks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Save Macro

Thanks, Tom. I was not activating the sheet first. I don't want to include
the code in a BeforeSave event, however, both because I'd prefer to keep all
code in one central file, and because I don't want to trigger the event on
every periodic save, just when you're ready to save prior to closing the
file. At other times, we'd prefer to press the Save icon, and continue
working where we are. The following works as expected. Thank you for your
quick and accurate response.

Public Sub SaveWorksheet()
On Error GoTo ErrorHandler
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
wrksht.Activate
Range("A1").Select
Next wrksht
ActiveWorkbook.Worksheets(1).Activate
ActiveWorkbook.Save

SubExit:
Exit Sub

ErrorHandler:
MsgBox "There has been the following error. Please contact the macro
administrator." & _
vbCrLf & Err.Number & " " & Err.Description
GoTo SubExit
End Sub

Sprinks

"Tom Ogilvy" wrote:

Use the before save event for the thisworkbook object/module

http://www.cpearson.com/excel/events.htm

Private Sub Workbook_BeforeSave( _
ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sh as Worksheet
ThisWorkbook.Activate
for each sh in thisworkbook.Worksheets
sh.Activate
sh.Range("A1").Select
Next
thisworkbook.worksheets(1).Select
Cancel = True
On Error goto ErrHandler
Application.EnableEvents = False
ThisWorkbook.Save
ErrHandler:
Application.EnableEvents = True
End Sub

This must be in the ThisWorkbook Module.

--
Regards,
Tom Ogilvy

"Sprinks" wrote in message
...
I'd like a macro that, prior to saving the current workbook, would

position
the cursor in cell A1, and make the first sheet the active one. The
following code does not work. Can anyone help?

Public Sub SaveWorksheet()
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
Range("A1").Select
Next wrksht
Worksheets(1).Select
ActiveWorkbook.Save
End Sub

Sprinks




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Save Macro

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
wrksht.Range("A1").Select
Next wrksht
Worksheets(1).Select
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)


"Sprinks" wrote in message
...
I'd like a macro that, prior to saving the current workbook, would

position
the cursor in cell A1, and make the first sheet the active one. The
following code does not work. Can anyone help?

Public Sub SaveWorksheet()
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
Range("A1").Select
Next wrksht
Worksheets(1).Select
ActiveWorkbook.Save
End Sub

Sprinks



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Save Macro

Thanks for your response, Bob, but this code will not work because the
Activate method is required to select each sheet.

Sprinks

"Bob Phillips" wrote:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
wrksht.Range("A1").Select
Next wrksht
Worksheets(1).Select
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)


"Sprinks" wrote in message
...
I'd like a macro that, prior to saving the current workbook, would

position
the cursor in cell A1, and make the first sheet the active one. The
following code does not work. Can anyone help?

Public Sub SaveWorksheet()
Dim wrksht As Worksheet
For Each wrksht In ActiveWorkbook.Worksheets
Range("A1").Select
Next wrksht
Worksheets(1).Select
ActiveWorkbook.Save
End Sub

Sprinks






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 to Insert Current Date into cell - Macro to "Save As" Guy[_2_] Excel Worksheet Functions 4 December 12th 08 08:20 PM
save as macro KJ Excel Discussion (Misc queries) 2 March 7th 08 04:37 PM
Macro to Save without the Save Message Ellen G Excel Discussion (Misc queries) 4 February 23rd 07 08:52 PM
ASP: Open Excel File with Macro, Allow Macro to run, and then save delgados129 Excel Programming 0 March 10th 05 09:35 PM
Prompted to save changes after macro save - why? Izar Arcturus Excel Programming 2 December 10th 03 09:27 PM


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