MmVv
MmVv

Reputation: 553

Save my WB based on another workbook name in VBA

I have a code which is doing following:

What I just need (if possible) is to save my wb in the same name as that external wb that I am taking data from and adding date/time at the end.

Example:

MainWB1.xlsm + ExternalWB1.xlsx >>> MainWB1.xlsx (This is now)

MainWB1.xlsm + ExternalWB1.xlsx >>> ExternalWB1_today().xlsx (This is what I wanna)

Upvotes: 7

Views: 297

Answers (3)

Cristian Buse
Cristian Buse

Reputation: 4608

You have 2 separate methods:

  • CopySheetFromClosedWorkbook2
  • SaveNoMacro

The name of the source workbook is only available in the scope of the CopySheetFromClosedWorkbook2 because that's where you open and close it. So, you have 2 options:

  1. Save the main workbook before exiting the scope of the CopySheetFromClosedWorkbook2 method i.e. while the name of the source book is available
  2. Save the name of the source book somewhere (global variable, named range, registy, custom xml part etc.) or even return it as a result (Function instead of Sub) so that you can call the SaveNoMacro method at a later stage

Save before exiting the scope

Here are 2 ways to do this:

  1. Place your save code before the src.Close False line so that you can use the src.Name property i.e. combine the 2 methods into one. Not sure if you want to do this
  2. Pass the name as an argument to the second method. In CopySheetFromClosedWorkbook2 replace this:
src.Close False

with this:

SaveNoMacro src.Name
src.Close False

and update SaveNoMacro to:

Sub SaveNoMacro(ByVal newName As String)
    Dim fn As String
    With ThisWorkbook
        fn = Replace(.FullName, .Name, Left(newName, InStrRev(newName, ".") - 1)) _
           & Format$(Date, "_yyyy-mm-dd") & ".xlsx"
        Application.DisplayAlerts = False
        .SaveAs fn, FileFormat:=xlWorkbookDefault
        Application.DisplayAlerts = True
    End With
    MsgBox "Saved as " & fn
End Sub

Save the name for later use

In case you don't want to run the 2 methods in a sequence then you can save the name for later use. Using a global variable is not a good idea as the state can be lost by the time you run the save method. Using a named range would work as long as you don't have your workbook protected i.e you can create a named range.

There are many options but the easiest to use is to write to registry using the built in SaveSetting option. Replace this:

src.Close False

with this:

SaveSetting "MyApp", "MySection", "NewBookName", src.Name
src.Close False

and update SaveNoMacro to:

Sub SaveNoMacro()
    Dim fn As String: fn = GetSetting("MyApp", "MySection", "NewBookName")
    If LenB(fn) = 0 Then
        MsgBox "No name was saved", vbInformation, "Cancelled"
        Exit Sub
    Else
        DeleteSetting "MyApp", "MySection", "NewBookName"
    End If
    With ThisWorkbook
        fn = Replace(.FullName, .Name, Left(fn, InStrRev(fn, ".") - 1)) _
           & Format$(Date, "_yyyy-mm-dd") & ".xlsx"
        Application.DisplayAlerts = False
        .SaveAs fn, FileFormat:=xlWorkbookDefault
        Application.DisplayAlerts = True
    End With
    MsgBox "Saved as " & fn
End Sub

Upvotes: 7

What I just need (if possible) is to save my wb in the same name as that external wb that I am taking data from and adding date/time at the end

You got the full path of your external wb in the variable FilePath so you can use that to save the workbook. You could save it like this (at the end of your sub CopySheetFromClosedWorkbook2):

Dim SaveName As String


SaveName = src.Path & "\" & Replace(Split(Filepath, "\")(UBound(Split(Filepath, "\"))), ".xlsm", Format(Date, "dd_mm_yyyy") & ".xlsx")


With ThisWorkbook
    Application.DisplayAlerts = False
    .SaveAs SaveName, FileFormat:=xlWorkbookDefault
    Application.DisplayAlerts = True
End With

Notice I'm using the object src to get the path where you want to save the new workbook, so you need to asign the line SaveName = .... anywhere before you do src.Close.

Upvotes: 1

david
david

Reputation: 2638

fn = Replace(.FullName, ".xlsm", ".xlsx")



fn = Replace(.FullName, ".xlsm", date & ".xlsx")

Upvotes: 1

Related Questions