Help with backup code

tscherf

New Member
Joined
Apr 22, 2015
Messages
14
I have a workbook and been trying to figure out how to do backup automatically.. Think i'm about to pull out some hair. I found some code on internet and put them together It does do a back up, but when I change the date on my system clock to test it again. nothing. I'm thinking because it sees that format still n my directory even though the date is different. not sure, truth I'm a newb and been trying work through this and learn same time. My final backup idea is, when the book opens check daily backup directory and if file there and check month directory is there a current month there skip if not create one. and same with year. I been searching ways to kill the fills after 30 days(daily), 12(monthly) and 7(years). I still having figured out the function work with it. I found code that I been playing with, but not sure how to run it to see if it even works. lol I'm a mess that's for sure.
This is what I have.

Private Sub Workbook_Open()
Worksheets("Home Menu").Activate
Application.DisplayAlerts = False
Dim sFile As String
sFile = "Daily " & Format(Now(), "m-d-yyyy") & ".xlsm"
If Dir("C:\Backup_L\Daily\") & sFile & Format(Now(), "m-d-yyyy.xlsm") <> "" Then
MsgBox "its there" 'here just to test, will be taken out
Else
ActiveWorkbook.SaveCopyAs Filename:="C:\Backup_L\Daily\" & sFile & ".xlsm"
End If
Application.DisplayAlerts = True
End Sub

not sure how put it in one of those boxes in the forms. Sorry
The function I think I deleted and went on a search for another. any help would be apperceive
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this...

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Workbook_Open()
    Worksheets("Home Menu").Activate
    Application.DisplayAlerts = [color=darkblue]False[/color]
    [color=darkblue]Dim[/color] sFile  [color=darkblue]As[/color] [color=darkblue]String[/color]
    sFile = "C:\Backup_L\Daily\Daily " & Format(Date, "m-d-yyyy") & ".xlsm"
    [color=darkblue]If[/color] Dir(sFile) <> "" [color=darkblue]Then[/color]
        MsgBox sFile, , "It Already Exists"  [color=green]'here just to test, will be taken out[/color]
    [color=darkblue]Else[/color]
        ActiveWorkbook.SaveCopyAs Filename:=sFile
        MsgBox sFile, , "Backup Saved"
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    Application.DisplayAlerts = [color=darkblue]True[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
im not sure if I needed to start a new thread or not, sorry if I was suppose to. I mentioned earlier I was on the search for something that would delete old files after so long as past. I found this code searching on your form. but it seems to be not working. I not sure if I figured out why or not. but this is the code.
Sub Delete_Old()
Dim A
Dim Dir_Path As String
Dir_Path = "C:\Backup_L\Daily\Daily"
Dim Masq_File, DIR_Result
Dim MyMONTH As String
'------------ LOOK IF ROOT DIRECTORY EXISTS
Masq_File = Dir_Path & "\" & "*.*"
DIR_Result = Dir(Masq_File, vbNormal) ' Init Dir_Result, We start with only files
While (DIR_Result <> "")
'------------ FILES EXIST -----------------
MyMONTH = Mid(DIR_Result, 15, 2) * 1
A = Day(Date) - 5
If (MyMONTH = Day(Date) - 5) Then
Kill Dir_Path & "\" & DIR_Result ' TO REMOVE the FILE
End If
DIR_Result = Dir
Wend

I'm thinking something with this but could be wrong.
Mid(DIR_Result, 15, 2) * 1
if I understand this code correctly. it takes list the directory starting 15 spots in for 2 spots. not sure what the *1 is yet. i'm hoping I can modify this daily, weekly, monthly, and yearly. Or I'm I wasting time?
Sorry to be a pain, I love doing this but just not good at it.
Tim
 
Upvote 0
This code intentionally deletes files. First test it on non-critical files.

Code:
[COLOR=darkblue]Sub[/COLOR] Delete_Old()
    
    [COLOR=darkblue]Dim[/COLOR] strPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], strFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] dCriteria [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Date[/COLOR], dFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Date[/COLOR], Counter [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    strPath = "C:\Backup_L\Daily\"
    
    [COLOR=darkblue]If[/COLOR] Dir(strPath, vbDirectory) = "" [COLOR=darkblue]Then[/COLOR]      [COLOR=green]'Test if folder exists[/COLOR]
        MsgBox strPath, vbInformation, "Folder Not Found"
    [COLOR=darkblue]Else[/COLOR]
        
        dCriteria = DateAdd("m", -1, Date)  [COLOR=green]'Criteria Date: one month previous[/COLOR]
        
        strFile = Dir(strPath & "Daily *", vbNormal)
        
        [COLOR=darkblue]While[/COLOR] strFile <> ""
            
            dFile = DateValue(Split(Split(strFile)(1), ".")(0))  [COLOR=green]'Date from the file name[/COLOR]
            
            [COLOR=darkblue]If[/COLOR] dFile < dCriteria [COLOR=darkblue]Then[/COLOR]
                Kill strPath & strFile    [COLOR=green]'Delete the file if older than criteria date[/COLOR]
                Counter = Counter + 1
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            
            strFile = Dir   [COLOR=green]'Next file[/COLOR]
            
        [COLOR=darkblue]Wend[/COLOR]
        
        MsgBox Counter & " files deleted. ", vbInformation, _
               "Delete Old File Names < " & Format(dCriteria, "mmm d, yyyy")
        
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Take note of my signature block below about the use of CODE tags; a.k.a. "Boxes"
 
Upvote 0
Thanks for the code. I get a mismatch error when I run it. I tried to debug the code and it errors out on the Kill strPath & strFile
does this have anything to do with the Dim? just trying to understand while I go through it.
thank you
 
Upvote 0
Thanks for the code. I get a mismatch error when I run it. I tried to debug the code and it errors out on the Kill strPath & strFile
does this have anything to do with the Dim? just trying to understand while I go through it.
thank you

Did you change anything in the code? If yes, show your code.

When it errors, hover the cursor over strPath and strFile. Their values should display in a pop-up. What are those values?
 
Upvote 0
No, I never changed anything. I just pasted and ran. :( when I used debug to cursor it errors at the kill line but if I close workbook and reopen it I click the debug and the
Code:
dFile = DateValue(Split(Split(strFile)(1), ".")(0))  'Date from the file name
is highlighted
 
Last edited:
Upvote 0
Do you have any other files in the same folder with their names starting with "Daily" but not having a date?

When it errors, hover the cursor over strFile. The value should display in a pop-up. What is the file name?
 
Upvote 0
OMG i'm an idiot Yes back when I was playing with mine, it had couple still left over. I deleted those and it worked perfect for me. Thank you so much. now I know asking a lot but could you explain to me why it did that, if you have that time? More I know easier this will get.
Thank you so much.....
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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