Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Reference active worksheet from macro in personal.xls

Well I just thought I was so clever as a non-programmer spending two
days writing this macro to save the current worksheet to two different
locations BUT ...

It worked fine in the workbook I wrote it in, then I learned that if I
put it in personal.xls all my workbooks could use it. I managed that
but now it backs up personal.xls not the workbook I execute it from.

I assume there is some way to reference the current workbook from a
macro living within personal.xls?

Also I suspect that there is a much better way to do this than doing
three saves?
Thanks
-----------------------
Sub CopyToTwoLocations()

Dim strFileA As String
Dim strFileB As String
Dim strFileC As String

'Save first just to be safe
ThisWorkbook.Save

'Save the name of the current doc
strFileA = ThisWorkbook.Name

'Capture the name of the current doc before the 'SveAs' changes it
strFileC = ThisWorkbook.FullName

'Define backup paths
strFileB = "C:\My Documents\SaveToTwoLocations\" & strFileA
strFileB2 = "H:\SaveToTwoLocations\" & strFileA

'Save backups
Application.DisplayAlerts = False ' Avoid msg for overwites
ThisWorkbook.SaveAs Filename:=strFileB

On Error GoTo MyError 'In case back up drive is off-line
ThisWorkbook.SaveAs Filename:=strFileB2

'Set the current (active) doc back to the original
ThisWorkbook.SaveAs Filename:=strFileC
Application.DisplayAlerts = True
Exit Sub

MyError:
MsgBox "Backup to flash apparently failed"
ThisWorkbook.SaveAs Filename:=strFileC
Application.DisplayAlerts = True
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Reference active worksheet from macro in personal.xls

ThisWorkbook is the workbook that owns the code--not the active workbook.

So when you moved the code into your personal.xls workbook, ThisWorkbook became
Personal.xls.

So try changing each occurence of ThisWorkbook to ActiveWorkbook:
strFileA = ThisWorkbook.Name
becomes
strFileA = ActiveWorkbook.Name

And excel's VBA has another way to save a backup. Then you don't have to do
that last .SaveAs to get back to the name you started with. (Check VBAs help
for .savecopyas.)

Option Explicit
Sub CopyToTwoLocations()

Dim strFileA As String
Dim strFileB As String
Dim strFileB2 As String

If ActiveWorkbook.Path = "" Then
MsgBox "Please save this workbook first!"
Exit Sub
End If

'Save first just to be safe
ActiveWorkbook.Save

'Save the name of the current doc
strFileA = ActiveWorkbook.Name

'Define backup paths
strFileB = "C:\My Documents\SaveToTwoLocations\" & strFileA
strFileB2 = "H:\SaveToTwoLocations\" & strFileA

'Save backups
On Error Resume Next
ActiveWorkbook.SaveCopyAs Filename:=strFileB
If Err.Number < 0 Then
MsgBox "Save to " & strFileB & " failed"
Err.Clear
End If
On Error GoTo 0

On Error Resume Next
ActiveWorkbook.SaveCopyAs Filename:=strFileB2
If Err.Number < 0 Then
MsgBox "Save to " & strFileB2 & " failed"
Err.Clear
End If
On Error GoTo 0

End Sub



livetohike wrote:

Well I just thought I was so clever as a non-programmer spending two
days writing this macro to save the current worksheet to two different
locations BUT ...

It worked fine in the workbook I wrote it in, then I learned that if I
put it in personal.xls all my workbooks could use it. I managed that
but now it backs up personal.xls not the workbook I execute it from.

I assume there is some way to reference the current workbook from a
macro living within personal.xls?

Also I suspect that there is a much better way to do this than doing
three saves?
Thanks
-----------------------
Sub CopyToTwoLocations()

Dim strFileA As String
Dim strFileB As String
Dim strFileC As String

'Save first just to be safe
ThisWorkbook.Save

'Save the name of the current doc
strFileA = ThisWorkbook.Name

'Capture the name of the current doc before the 'SveAs' changes it
strFileC = ThisWorkbook.FullName

'Define backup paths
strFileB = "C:\My Documents\SaveToTwoLocations\" & strFileA
strFileB2 = "H:\SaveToTwoLocations\" & strFileA

'Save backups
Application.DisplayAlerts = False ' Avoid msg for overwites
ThisWorkbook.SaveAs Filename:=strFileB

On Error GoTo MyError 'In case back up drive is off-line
ThisWorkbook.SaveAs Filename:=strFileB2

'Set the current (active) doc back to the original
ThisWorkbook.SaveAs Filename:=strFileC
Application.DisplayAlerts = True
Exit Sub

MyError:
MsgBox "Backup to flash apparently failed"
ThisWorkbook.SaveAs Filename:=strFileC
Application.DisplayAlerts = True
End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Reference active worksheet from macro in personal.xls

On May 5, 8:11 pm, Dave Peterson wrote:
ThisWorkbook is the workbook that owns the code--not the active workbook.

So when you moved the code into your personal.xls workbook, ThisWorkbook became
Personal.xls.

So try changing each occurence of ThisWorkbook to ActiveWorkbook:
strFileA = ThisWorkbook.Name
becomes
strFileA = ActiveWorkbook.Name

And excel's VBA has another way to save a backup. Then you don't have to do
that last .SaveAs to get back to the name you started with. (Check VBAs help
for .savecopyas.)

Option Explicit
Sub CopyToTwoLocations()

Dim strFileA As String
Dim strFileB As String
Dim strFileB2 As String

If ActiveWorkbook.Path = "" Then
MsgBox "Please save this workbook first!"
Exit Sub
End If

'Save first just to be safe
ActiveWorkbook.Save

'Save the name of the current doc
strFileA = ActiveWorkbook.Name

'Define backup paths
strFileB = "C:\My Documents\SaveToTwoLocations\" & strFileA
strFileB2 = "H:\SaveToTwoLocations\" & strFileA

'Save backups
On Error Resume Next
ActiveWorkbook.SaveCopyAs Filename:=strFileB
If Err.Number < 0 Then
MsgBox "Save to " & strFileB & " failed"
Err.Clear
End If
On Error GoTo 0

On Error Resume Next
ActiveWorkbook.SaveCopyAs Filename:=strFileB2
If Err.Number < 0 Then
MsgBox "Save to " & strFileB2 & " failed"
Err.Clear
End If
On Error GoTo 0

End Sub



livetohikewrote:

Well I just thought I was so clever as a non-programmer spending two
days writing this macro to save the current worksheet to two different
locations BUT ...


It worked fine in the workbook I wrote it in, then I learned that if I
put it in personal.xls all my workbooks could use it. I managed that
but now it backs up personal.xls not the workbook I execute it from.


I assume there is some way to reference the current workbook from a
macro living within personal.xls?


Also I suspect that there is a much better way to do this than doing
three saves?
Thanks
-----------------------
Sub CopyToTwoLocations()


Dim strFileA As String
Dim strFileB As String
Dim strFileC As String


'Save first just to be safe
ThisWorkbook.Save


'Save the name of the current doc
strFileA = ThisWorkbook.Name


'Capture the name of the current doc before the 'SveAs' changes it
strFileC = ThisWorkbook.FullName


'Define backup paths
strFileB = "C:\My Documents\SaveToTwoLocations\" & strFileA
strFileB2 = "H:\SaveToTwoLocations\" & strFileA


'Save backups
Application.DisplayAlerts = False ' Avoid msg for overwites
ThisWorkbook.SaveAs Filename:=strFileB


On Error GoTo MyError 'In case back up drive is off-line
ThisWorkbook.SaveAs Filename:=strFileB2


'Set the current (active) doc back to the original
ThisWorkbook.SaveAs Filename:=strFileC
Application.DisplayAlerts = True
Exit Sub


MyError:
MsgBox "Backup to flash apparently failed"
ThisWorkbook.SaveAs Filename:=strFileC
Application.DisplayAlerts = True
End Sub


--

Dave Peterson


Just perfect!
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
Active cell as a reference to open a worksheet LaDdIe Excel Worksheet Functions 6 March 15th 07 07:06 PM
Altering code to reference the worksheet before the active worksheet KimberlyC Excel Programming 8 March 15th 05 10:26 PM
Assigning a macro to a worksheet (vs personal.xls) Bridget[_3_] Excel Programming 4 May 15th 04 07:22 PM
How to end macro on inital active worksheet containing macro button that was clicked Silverhawk1 Excel Programming 2 May 14th 04 03:58 PM
macro to apply worksheet event to active worksheet Paul Simon[_2_] Excel Programming 3 August 7th 03 02:50 AM


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