hi,
The below is a coding approach I use frequently (copied & pasted w/o the guts of the macro - hopefully I haven't deleted too much) & most of the ease comes from Ken Pul's function which is included below...
Option Explicit
Private Sub CreatingDailyFile()
Application.ScreenUpdating = False
Dim RawCSVDataFolder As String, StartOfFolderPathForSaving As String, LastFoldersInPathForSaving As String, File_name As String
'''definitions of variables used
''File strings
RawCSVDataFolder = "S:\blahblah\blah\"
StartOfFolderPathForSaving = "S:\xyz\abc\"
LastFoldersInPathForSaving = Year(Date) & "\" & Month(Date) & "_" & MonthName(Month(Date), True) & " " & Year(Date) & "\"
File_name = Format(Date, "yyyymmdd") & " Created File"
'To check on existence of folders & files and create if required
If DoesFileFolderExist(StartOfFolderPathForSaving & LastFoldersInPathForSaving & File_name & ".xls") Then _
MsgBox "Daily SOH already exists therefore macro ending & macro file closing.", , "INFO ONLY": IsMacroComplete = True: GoTo ExitSub
If Not DoesFileFolderExist(StartOfFolderPathForSaving & Year(Date)) Then MkDir (StartOfFolderPathForSaving & Year(Date))
If Not DoesFileFolderExist(StartOfFolderPathForSaving & LastFoldersInPathForSaving) Then _
MkDir (StartOfFolderPathForSaving & LastFoldersInPathForSaving)
'to check the existence of the raw data extract, open it & id the last row of data
If Not DoesFileFolderExist(RawCSVDataFolder & File_name & ".csv") Then _
MsgBox "Raw data extract does not exist therefore macro ending.", , "INFO ONLY": IsMacroComplete = False: GoTo ExitSub
Workbooks.Open Filename:=RawCSVDataFolder & File_name & ".csv"
'insert the guts of your macro here...
'Saves the daily file in xls format.
ActiveWorkbook.SaveAs Filename:= _
StartOfFolderPathForSaving & LastFoldersInPathForSaving & File_name & ".xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:=FilePassWord, _
ReadOnlyRecommended:=False, CreateBackup:=False
'to advise macro is finished (& macro file closing) or otherwise.
MsgBox "Daily file now created & saved - therefore macro file will close.", , " FILE NOW CREATED"
ExitSub:
Application.ScreenUpdating = True
If IsMacroComplete Then
ThisWorkbook.Close False
Else
MsgBox "Daily file not created yet - therefore macro file staying open.", , "FILE NOT CREATED!"
End If
End Sub
Private Function DoesFileFolderExist(strfullpath As String) As Boolean
'sourced from www.excelguru.ca/node/30 by Ken Puls
'note it only checks for the existence of the lowest folder (or the file) in the strfullpath string.
If Not Dir(strfullpath, vbDirectory) = vbNullString Then DoesFileFolderExist = True
End Function
hth
Rob
Bookmarks