PDA

View Full Version : Run macro save workbook under new name then open original workbook again.


rsdjimbie
Aug 23, 2012, 10:51 PM
Run macro save workbook under new name then open original workbook again to continue other code to run.I have the following code that runs x3 "Application.Run" from the original, but is not effective.

Sub SaveWorkbookAsNewFile()
Dim ActSheet As Worksheet
Dim ActBook As Workbook
Dim CurrentFile As String
Dim NewFileType As String
Dim NewFile As String

Application.ScreenUpdating = False ' Prevents screen refreshing.

CurrentFile = ThisWorkbook.FullName

NewFileType = "Excel Files 1997-2003 (*.xls), *.xls," & _
"Excel Files 2007 (*.xlsx), *.xlsx," & _
"All files (*.*), *.*"

NewFile = Application.GetSaveAsFilename( _
InitialFileName:=NewFileName, _
fileFilter:=NewFileType)

If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs Filename:=NewFile, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False

Application.Run "Advise_Manhood_PAYE_Advise"

Set ActBook = ActiveWorkbook
Workbooks.Open CurrentFile
ActBook.Close
End If


CurrentFile = ThisWorkbook.FullName

NewFileType = "Excel Files 1997-2003 (*.xls), *.xls," & _
"Excel Files 2007 (*.xlsx), *.xlsx," & _
"All files (*.*), *.*"

NewFile = Application.GetSaveAsFilename( _
InitialFileName:=NewFileName, _
fileFilter:=NewFileType)

If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs Filename:=NewFile, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False


Application.Run "Advise_Manhood_Wage_Advise"

Set ActBook = ActiveWorkbook
Workbooks.Open CurrentFile
ActBook.Close
End If

CurrentFile = ThisWorkbook.FullName

NewFileType = "Excel Files 1997-2003 (*.xls), *.xls," & _
"Excel Files 2007 (*.xlsx), *.xlsx," & _
"All files (*.*), *.*"

NewFile = Application.GetSaveAsFilename( _
InitialFileName:=NewFileName, _
fileFilter:=NewFileType)

If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs Filename:=NewFile, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
End If

Application.Run "BankAdviceToDesktop"

Application.ScreenUpdating = True
End Sub

Thanks in advance.

JBeaucaire
Aug 24, 2012, 12:01 AM
Perhaps it would be less confusing to simply save the active workbook using the SaveCopyAs method to save a copy without affecting the current workbook itself, then it's child's play to continue on with the next macro.

rsdjimbie
Aug 25, 2012, 09:54 AM
Thanks this is a much shoter version and it all runs well, thanks JB. Only problem I have is when the ActiveWorkbook's name change is the code giving a file not found error.

Option Explicit
Sub All_Three_Advise_Macro_Runs()
Dim Thisbook As String

ActiveWorkbook.SaveCopyAs ("TempAdvise.xls")

Thisbook = ActiveWorkbook.Name

Application.Run "Advise_Manhood_Wage_Advise" ' mod 17

Workbooks.Open Filename:=(Thisbook)

Application.Run "Advise_Manhood_PAYE_Advise" 'mod 20

Workbooks.Open Filename:=(Thisbook)

Application.Run "BankAdviceToDesktop" ' mod 10

Application.Quit
End Sub

rsdjimbie
Aug 25, 2012, 01:07 PM
Found my problem, called the active workbook instaed of the temp file, changed this lin and it works.

Thisbook = ("TempAdvise.xls")