Leith gave me this piece of code a couple of weeks ago which is brilliant. Is there a way to write a Dim statement to automate this part of the macro.
The macro is fine like this but it is only good for the month of May. I have added MonthID in the filepath below to direct it to the May, June etc Sub Directories.FileName = Dir(FilePath & "\*MAY FAB EST*.xls")
I was thinking of something like this but I can't seem to get it to slot into the first piece of code.FilePath = "J:\Weekly Sales Summaries\D&C\Fab Report\CRAFT FAB 2011\" + MonthID + ""
But when I put MonthID1 into the code as below, it doesn't work. Is it because of the wildcards?If MonthID = 6 Then MonthID1 = *JUN FAB EXT*.XLS End If
ThanksFileName = Dir(FilePath & "\" & MonthID1)
Full Macro below
FilePath = "J:\Weekly Sales Summaries\D&C\Fab Report\CRAFT FAB 2011\" + MonthID + "" FileName = Dir(FilePath & "\*MAY FAB EST*.xls") Set RegExp = CreateObject("VBScript.RegExp") RegExp.Pattern = "(.*)(\d{6})(.+)" Do While FileName <> "" If RegExp.Test(FileName) Then fnDate = RegExp.Replace(FileName, "$2") D = Left(fnDate, 2) M = Mid(fnDate, 3, 2) Y = Right(fnDate, 2) fnDate = DateSerial(Y, M, D) If fnDate > LastDate Then LastDate = fnDate LastFile = FileName End If End If FileName = Dir() Loop If LastFile <> "" Then Workbooks.Open FilePath & "\" & LastFile NewDate = Format(CDate(LastDate) + 7, "ddmmyy") NewName = RegExp.Replace(LastFile, "$1" & NewDate & "$3") ActiveWorkbook.SaveCopyAs FilePath & "\" & NewName ActiveWorkbook.Close SaveChanges:=False Workbooks.Open FilePath & "\" & NewName End If
Last edited by timbo1957; 06-21-2011 at 09:34 AM.
Maybe something like:
Filename = Dir(FilePath & "\*" & Format(DateSerial(1900, MonthID, 1), "MMM") & " FAB EST*.xls)
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Thanks Dom,
When I run that Filename = " FAB EST.xls" it misses off the "JUN" in front of it.
If you name every file according it's week, there's no need to create separate folders for every month.
Using the ISO-standard (yyyymmdd) makes your code more robust (besides the curious Excel behaviour handling dates).
Shortly: 'structuring precedes coding'
The latest file can be found using a loop from the present day backwards. Using separate folders for each month complicates the code unnecessary and adds no functionality.
There's no need to open a workbook to produce it's copy.
sub snb() c00 = "J:\Weekly Sales Summaries\D&C\Fab Report\CRAFT FAB\" x=0 do c01 = Dir(c00 & format(date-x,"yyyymmdd") & " FAB EST.xls") x=x+1 loop until c01<>"" copyfile c00 & c01,c00 & format(cdate(left(c01,8)+7),"yyyymmdd") & mid(c01,9) End Sub
Last edited by snb; 06-02-2011 at 06:44 AM.
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Hi Dom,
Sorry been in a meeting. If I Step through the macro and hover over MonthID it has picked up the Month I get Type Mismatch.
Dim strFileName As String Dim strFilePath As String Dim lngMonthID As Long strFilePath = "H:" lngMonthID = 6 'just to show the file name and path MsgBox strFilePath & "\*" & Format(DateSerial(1900, lngMonthID, 1), "MMM") & " FAB EST*.xls" strFileName = Dir(strFilePath & "\*" & Format(DateSerial(1900, lngMonthID, 1), "MMM") & " FAB EST*.xls")
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Thanks Domski.
I know Domski already provided a solution, but here is why your original code did not work.
Originally Posted by timbo1957
It should be:
If MonthID = 6 Then MonthID1 = "*JUN FAB EXT*.XLS" End If
Hi Whizbang,
Thanks for the post. How would this change this line of code?
I have triedFileName = Dir(FilePath & "\*JUN FAB EST*.xls")
But when I step through the macro the Filenname = ""FileName = Dir(FilePath & "\" & MonthID1)
Without testing the code, I would say that Filename is returning "" because no file exists at Filepath & "\" & MonthID1. Dir() is looking at the path you provide and returning the first filename that matches the path you privide. If no file matches, Dir() returns a null string.
See this link for a more thorough description:
http://www.techonthenet.com/excel/formulas/dir.php
One thing I noticed is that the original filename was "*JUN FAB EXT*.XLS" and the filename in your last post was "*JUN FAB EST*.xls"
EST should be EXT, or vice versa.
Last edited by Whizbang; 06-09-2011 at 11:50 AM.
Hi Whizbang,
I have corrected the error and now the macro runs but it is picking up the wrong file.
Their are three files in the folder
JUN FAB EST 060611
JUN FAB EST 300511
JUN FAB EST MASTER
It is opening 300511 instead of 060611.
I tried changing this bit of code
tofnDate = DateSerial(Y, M, D)
That opens the right file but saves it as JUN FAB EST 180611 instead of JUN FAB EST 130611.fnDate = DateSerial(D, M, Y)
Full macro for this part below.
Dim D As Integer Dim FileName As String Dim FilePath As String Dim fnDate As String Dim LastDate As Variant Dim LastDate1 As String Dim LastDate2 As String Dim LastFile As String Dim M As Integer Dim NewDate As String Dim NewName As String Dim RegExp As Object Dim Y As Integer Dim LastWeek Dim ThisWeek Dim ThisMonth Sub Weekly_Dues09() WEEKLY_DUES_DATES_01.WEEKLY_DUES_DATES_01 ' Open Last Weeks Craft Fab And Save As This Weeks. ' Copy Last Weeks Weekly Sales To This Week Amend Week Number. ' Copy Last FL Sales To This Week Amend File Name ' Copy Last TA Sales To This Week Amend File Name ' Copy Last BL Sales To This Week Amend File Name MsgBox ("Weekly Dues Stage 09 Will Start When you Click OK") FilePath = "J:\Weekly Sales Summaries\D&C\Fab Report\CRAFT FAB 2011\" + MonthID + "" FileName = Dir(FilePath & "\" & MonthID1) Set RegExp = CreateObject("VBScript.RegExp") RegExp.Pattern = "(.*)(\d{6})(.+)" Do While FileName <> "" If RegExp.Test(FileName) Then fnDate = RegExp.Replace(FileName, "$2") D = Left(fnDate, 2) M = Mid(fnDate, 3, 2) Y = Right(fnDate, 2) fnDate = DateSerial(Y, M, D) If fnDate > LastDate Then LastDate = fnDate LastFile = FileName End If End If FileName = Dir() Loop If LastFile <> "" Then Workbooks.Open FilePath & "\" & LastFile NewDate = Format(CDate(LastDate) + 7, "ddmmyy") NewName = RegExp.Replace(LastFile, "$1" & NewDate & "$3") ActiveWorkbook.SaveCopyAs FilePath & "\" & NewName ActiveWorkbook.Close SaveChanges:=False Workbooks.Open FilePath & "\" & NewName End If LastDate1 = Right(LastFile, 10) LastDate2 = Left(LastDate1, 6) End Sub
What is the date format you want your filename to end with? To me, as an American 060611 means "June 06, 2011". 130611 just makes no sense at all.
Regardless, I would reevaluate your date scheme you use to name your files. What you want is not readability, but sortability. If you name your files in the format ddmmyy, it will sort so that the first of each and every month comes before the second of each and every month. If you name them mmddyy, then this means all your January files, of each and every year, will be before your February files. This can make finding specific files take longer than necessary.
Really, when naming files with dates, it is best to go with yyyymmdd. That way, they sort from oldest to newest very neatly. The reason you want this neat sorting is because functions like DIR() look at filenames in ascending order. But also, if you are using wildcards, it will return the closest match. This can cause headaches for your coding to try and figure out in what order your file will appear and how to arrange your wildcard search string.
For instance, you said you tried this code first:
, but your filename was 060611fnDate = DateSerial(Y, M, D)
When DIR() tries to find the first match, it takes the string 110606 and goes down the filelist. It cannot find an exact match, so it finds the closest match. 300511 is much closer to 110606 than 060611.
So you changed the code to:
and got your file 060611. But what would have happened if somehow you got your days mixed up and there was no 060611 file, but there was a 060610 file? Of course it would have given you 060610. But you and I know there is a whole year's difference between the two. Wouldn't you rather search for 110606 and get 110605?fnDate = DateSerial(D, M, Y)
Last edited by Whizbang; 06-10-2011 at 08:43 AM.
Hi Whizbang,
It's the UK date format 060611 = ddmmyy.
So the filenames will always be saved with ddmmyy at the end of the filename. I have no control over that. What parts of the macro do I need to change so that the macro realises that if there are two files in a directory one with a filename ending in 300511 and another ending in 060611 that the 060611 file is the most recent? and that that is the one to open?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks