+ Reply to Thread
Results 1 to 15 of 15

Thread: Dim Statement

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Dim Statement

    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.

        FileName = Dir(FilePath & "\*MAY FAB EST*.xls")
    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.

    FilePath = "J:\Weekly Sales Summaries\D&C\Fab Report\CRAFT FAB 2011\" + MonthID + ""
    I was thinking of something like this but I can't seem to get it to slot into the first piece of code.

    If MonthID = 6 Then 
    MonthID1 = *JUN FAB EXT*.XLS 
    End If
    But when I put MonthID1 into the code as below, it doesn't work. Is it because of the wildcards?

        FileName = Dir(FilePath & "\" & MonthID1)
    Thanks

    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.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Dim Statement

    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.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Re: Dim Statement

    Thanks Dom,

    When I run that Filename = " FAB EST.xls" it misses off the "JUN" in front of it.

  4. #4
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Nor dim nor regular expressions

    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.



  5. #5
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Dim Statement

    Quote Originally Posted by timbo1957 View Post
    Thanks Dom,

    When I run that Filename = " FAB EST.xls" it misses off the "JUN" in front of it.
    Had you assigned a value to MonthID?

    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.

  6. #6
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Re: Dim Statement

    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.

  7. #7
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Dim Statement

    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.

  8. #8
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Re: Dim Statement

    Thanks Domski.

  9. #9
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: Dim Statement

    I know Domski already provided a solution, but here is why your original code did not work.

    Quote Originally Posted by timbo1957
    If MonthID = 6 Then 
    MonthID1 = *JUN FAB EXT*.XLS 
    End If

    It should be:
    If MonthID = 6 Then 
    MonthID1 = "*JUN FAB EXT*.XLS"
    End If

  10. #10
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Re: Dim Statement

    Hi Whizbang,

    Thanks for the post. How would this change this line of code?

    FileName = Dir(FilePath & "\*JUN FAB EST*.xls")
    I have tried

    FileName = Dir(FilePath & "\" & MonthID1)
    But when I step through the macro the Filenname = ""

  11. #11
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: Dim Statement

    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

  12. #12
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: Dim Statement

    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.

  13. #13
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Re: Dim Statement

    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

    fnDate = DateSerial(Y, M, D)
    to

    fnDate = DateSerial(D, M, Y)
    That opens the right file but saves it as JUN FAB EST 180611 instead of JUN FAB EST 130611.

    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

  14. #14
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: Dim Statement

    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:
    fnDate = DateSerial(Y, M, D)
    , but your filename was 060611
    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:
    fnDate = DateSerial(D, M, Y)
    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?
    Last edited by Whizbang; 06-10-2011 at 08:43 AM.

  15. #15
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro 2003
    Posts
    209

    Re: Dim Statement

    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?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0