If/Then Logic For VBA Save As functionality?

mjd

Board Regular
Joined
Feb 23, 2010
Messages
73
Hello,

I am working on a project in Excel 2010 that will automatically save a file using a specific naming protocol, based off the dates included on the spreadsheet.

Right now, I have the following code working perfectly for today's date

Code:
Sub Save()


Dim dtDate As Date
dtDate = Date


Dim strFile As String
strFile = "H:\Trade Files\" & Format(dtDate, "mm-dd") & " " & "Bank" & ".xlsx"


ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
    :=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = False

Now, what I'm hoping to do, is to include logic that if a certain cell [say B3 for this example] contains a date that is not equal to today's date, to instead name the file as "[date in referenced cell] As Of Trades - Bank.xlsx"

Simplifying matters is that the source report will never co-mingle data from multiple dates, so it will really be an either/or situation in terms of the file naming protocol.

I've searched high and low and haven't had much success finding any resource on conditional save-as functions. Is this even possible in excel vba?

Thank a million!

Mike
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Mike

Can't you just set dtDate to the value in B3?
 
Upvote 0
I could, the major issue for me is the difference in the text component "mm-dd Bank.xlsx" for today's data versus "previous mm-dd As-Of Trades Bank.xlsx" for a prior day's activity. Getting the if/then logic in there is what is tripping me up.

Thanks,
Mike
 
Upvote 0
Perhaps.
Code:
Sub MySave()
Dim dtDate As Date
Dim strFile As String
Dim strPath As String

    strPath = "H:\Trade Files\"

    dtDate = Date

    If dtDate = Range("B3").Value Then
        strFile = Format(dtDate, "mm-dd") & " Bank.xlsx"
    Else
        strFile = Format(Range("B3").Value, "mm-dd") & " As Of Trades - Bank.xlsx"
    End If

    ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat _
    :=xlOpenXMLWorkbook, CreateBackup:=False

    Application.DisplayAlerts = False
 
Upvote 0
Norie,

Thank works beautifully! Thank you so very much!

Mike

Perhaps.
Code:
Sub MySave()
Dim dtDate As Date
Dim strFile As String
Dim strPath As String

    strPath = "H:\Trade Files\"

    dtDate = Date

    If dtDate = Range("B3").Value Then
        strFile = Format(dtDate, "mm-dd") & " Bank.xlsx"
    Else
        strFile = Format(Range("B3").Value, "mm-dd") & " As Of Trades - Bank.xlsx"
    End If

    ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat _
    :=xlOpenXMLWorkbook, CreateBackup:=False

    Application.DisplayAlerts = False
 
Upvote 0

Forum statistics

Threads
1,215,356
Messages
6,124,471
Members
449,163
Latest member
kshealy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top