Results 1 to 3 of 3

need help to get vba working after changes to some property format

Threaded View

  1. #1
    Registered User
    Join Date
    12-19-2012
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    2

    need help to get vba working after changes to some property format

    Hello Excel geniuses...

    I have the following code the works well when the property list contains alpha-numeric but won't work if the property code is only numbers. The alpha numeric property code in the list will have something like CE1001, DE1005. All these property codes are to be changed in the new year to appear as 12398, 45667, 11134 etc. Before, if run the macro with the alphanumeric property code all the file will be there.

    ----------------------

    Sub LoadFilesByLastModifiedDate()
        
      'loading data
        Dim startDate As String
        Dim endDate As String
        Dim filePath As String
        ' mm/dd/year
        startDate = "12/15/2012"
        endDate = "12/22/2012"
        
        filePath = "C:\Documents and Settings\fi\Desktop\Payroll Data"
        
        Workbooks("Payroll Processing Macro.xls").Worksheets("dataFile").Columns("A:D").ClearContents
        
        Call RecursiveDir(startDate, endDate, filePath)
        'MsgBox (m)
        'Call RecursiveDir("S:\Payroll\JOURNAL ENTRIES\2010")
      Dim kk As Integer
      Dim Number As Integer
      
      kk = Application.CountA(Worksheets("homeInput").Range("A1:P1000").Columns(1))
      Number = Application.CountA(Worksheets("dataFile").Range("A1:E1000").Columns(1))
    
      Dim Dir() As String
      Dim fileName() As String
      
      ReDim Dir(1 To Number)
      ReDim fileName(1 To Number)
      
      Dim NewFileName As String
      
      For m = 1 To Number
        Dir(m) = Worksheets("dataFile").Range("A1").Offset(m - 1, 0).Value
        fileName(m) = Worksheets("dataFile").Range("B1").Offset(m - 1, 0).Value
      Next m
    
      
      Dim filelist() As String
      Dim Properties() As String
      
      ReDim filelist(1 To kk) As String
      ReDim Properties(1 To kk) As String
        
      For m = 1 To kk
        Properties(m) = Worksheets("homeInput").Range("A1").Offset(m - 1, 0).Value
      Next m
      
      Dim Files As String
      For m = 1 To Number
        Files = Dir(m) & fileName(m)
          
          For i = 1 To kk
            If StringExistsInFile(Properties(i), Files) Then
              If StringExistsInFile("finished", Files) Or StringExistsInFile("done", Files) Or StringExistsInFile("good", Files) Then
                MsgBox ("File " + Properties(i) + " is not good.")
              Else
            
              NewFileName = Str(i) + " " + fileName(m)
              Name Files As Dir(m) & NewFileName
              filelist(i) = Dir(m) & NewFileName
              
              Worksheets("dataFile").Range("C1").Offset(i - 1, 0).Value = filelist(i)
              Worksheets("dataFile").Range("D1").Offset(i - 1, 0).Value = Properties(i)
              End If
              
             End If
             
          Next
      Next
        
    End Sub
    
    Public Sub RecursiveDir(ByVal sd As String, ByVal ed As String, ByVal CurrDir As String, Optional ByVal Level As Long)
        Dim Dirs() As String
        Dim NumDirs As Long
        Dim fileName As String
        'Dim NewFileName As String
        Dim PathAndName As String
        'Dim NewPathAndName As String
        Dim i As Long
        
    '   Make sure path ends in backslash
        If Right(CurrDir, 1) <> "\" Then CurrDir = CurrDir & "\"
    
    '   Put column headings on active sheet
        'Cells(1, 1) = "Path"
        'Cells(1, 2) = "Date/Time"
    
    '   Get files
        fileName = Dir(CurrDir & "*.*", vbDirectory)
        Do While Len(fileName) <> 0
          If Left(fileName, 1) <> "." Then 'Current dir
            PathAndName = CurrDir & fileName
        
            If (GetAttr(PathAndName) And vbDirectory) = vbDirectory Then
              'store found directories
               ReDim Preserve Dirs(0 To NumDirs) As String
               Dirs(NumDirs) = PathAndName
               NumDirs = NumDirs + 1
               
               'DateLastModified
               
               ElseIf FileDateTime(PathAndName) > DateValue(sd) And FileDateTime(PathAndName) <= DateValue(ed) Then
              'Write the path and file to the sheet
                 'NewFileName = Str(n) + " " + FileName
                 'NewPathAndName = CurrDir & NewFileName
                 
                 'Name CurrDir & FileName As CurrDir & NewFileName
                 
                 Cells(WorksheetFunction.CountA(Range("A:A")) + 1, 1) = _
                    CurrDir
                  Cells(WorksheetFunction.CountA(Range("B:B")) + 1, 2) = _
                  fileName
                  
                  'n = n + 1
                  
                  End If
             End If
            fileName = Dir()
        Loop
        ' Process the found directories, recursively
        For i = 0 To NumDirs - 1
            RecursiveDir sd, ed, Dirs(i), Level + 2
        Next i
    End Sub
    
    Public Function StringExistsInFile(TheString As String, TheFile As String) As Boolean
      Dim L As Long, s As String, FileNum As Integer
      FileNum = FreeFile
      Open TheFile For Binary Access Read Shared As #FileNum
      L = LOF(FileNum)
      s = Space$(L)
      Get #1, , s
      Close #FileNum
      If InStr(1, s, TheString) Then
        StringExistsInFile = True
      End If
    End Function
    Last edited by farainm; 12-21-2012 at 11:48 AM.

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.6.0 RC 1