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
 
The code extracts the date from the file name Daily m-d-yyyy.xlsm. If there are other files that start with Daily but have no date, it errors because it assumes all "Daily" file names include dates. A test could be added, if needed, to avoid the missing-date-in-the-file-name error.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
thank you again. I know
Code:
dFile = DateValue(Split(Split(strFile)(1), ".")(0))[\code] as to do with extracting the date. I been researching what split does and I know it splits the file you defined. But I don't understand does it just split daily from the date?  I'm thinking first split splits daily 4/1/15.xlsm to daily 4/1/15   .. second split separated daily and the date?  I hope i'm going in the right direction, because trying to convert to work in my monthly and yearly folder.  what part of that line tells it to look at the month, well singles it out with rest of the file name. trying to figure out what (1) does.  guessing (0) says nothing after, or read in binary.    Sorry for bugging you.
 
Upvote 0
thank you again. I know
Code:
dFile = DateValue(Split(Split(strFile)(1), ".")(0))[\code] as to do with extracting the date. I been researching what split does and I know it splits the file you defined. But I don't understand does it just split daily from the date?  I'm thinking first split splits daily 4/1/15.xlsm to daily 4/1/15   .. second split separated daily and the date?  I hope i'm going in the right direction, because trying to convert to work in my monthly and yearly folder.  what part of that line tells it to look at the month, well singles it out with rest of the file name. trying to figure out what (1) does.  guessing (0) says nothing after, or read in binary.    Sorry for bugging you.[/QUOTE]

The [I]Split[/I] function splits text (your [I]File Name[/I] in this case) based on a delimiter. If no delimiter is specified, the default delimiter is the space character. The result from a split is a zero-based array.
[FONT=courier new]
dFile = DateValue([COLOR=#0000FF]Split([/COLOR][COLOR=#FF0000]Split(strFile)(1)[/COLOR][COLOR=#0000FF], ".")(0)[/COLOR])[/FONT]

The 1st split (red) specifies no delimiter; it splits at spaces. So "Daily 1/1/2015.xlsm" splits into an array of "Daily" =  element (0) and "1/1/2015.xlsm" = element(1). It then references element (1) for use in the second split.

The second split (blue) uses "." as the delimiter. It splits element (1) "1/1/2015.xlsm" from previous split and returns the first element (0) of this split which is the just the date as text.

[I]DateValue[/I] then converts the text-date result into a serial date.

Once you have the serial date ([I]dFile[/I]), you can use...
[FONT=Courier New]Month(dFile)[/FONT] to get the month number (1 to 12)
[FONT=Courier New]Format(dFile, "mmmm")[/FONT] to get the month's full name
[FONT=Courier New]Year(dFile)[/FONT] to get the year.


This is but one method to parse the Date based on your current file naming convention. There are several other ways it could be done. Pretty much anyone could point you in the right direction given an example text pattern and the desired result.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

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