Thread: Save As code
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Kevin B Kevin B is offline
external usenet poster
 
Posts: 1,316
Default Save As code

The following macro verifies that there is a date in B2 in the sheet named
ABC, and if not it uses the current date. The date is formatted as text with
dashes between the month-day-year as the "/" is an invalid character in a
filename:

Sub SaveWithDate()
'
Dim wb As Workbook
Dim ws As Worksheet
Dim varVal As Variant
Dim strFileName As String

Set wb = ActiveWorkbook
Set ws = wb.Worksheets("ABC")
varVal = ws.Range("B2").Value

If IsDate(varVal) Then
strFileName = "DataFile_" & Format(CStr(varVal), "mm-dd-yyyy") &
".xls"
Else
strFileName = "DataFile_" & Format(CStr(Date), "mm-dd-yyyy") & ".xls"
End If

ActiveWorkbook.SaveAs Filename:=strFileName

Set wb = Nothing
Set ws = Nothing

End Sub

--
Kevin Backmann


"Aaron" wrote:

Hi,

I am looking to automate my daily Save As. I create an updated workbook
every day that needs to be saved with a name and Date. The name XYZ is the
same, but it is followed with a Date that is located in Cell B2 of sheet ABC.
Does anyone know if I can create a macro that will save my file with the
name and date located in cell b2? Thank you in advance.