+ Reply to Thread
Results 1 to 3 of 3

Macro to list files containing TB within a specified date range in C:\My Documents

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Macro to list files containing TB within a specified date range in C:\My Documents

    I have tried to write code to list files on sheet "Macro" containing "TB" in C:\My documents for eg bR1TB (Nert).xls . SouternTB.xls etc within a speciied date range in D2:E2 (Sheet Macro)


    These last date modified was 07/03/2023 (format dd/mm/yyyy)

    However when running the macro no files have been extracted

    It would be appreciated if someone could kindly amend my code

     Sub ListFiles()
        Dim objFSO As Object
        Dim objFolder As Object
        Dim objFile As Object
        Dim i As Integer
        Dim strSearch As String
        Dim strFolder As String
        
        'Set the folder path to search
        strFolder = "C:\my documents\"
        
        'Set the search string to look for
        strSearch = "*TB*"
        
        'Set up the file system object
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(strFolder)
        
        'Loop through the files in the folder and add to the list if they match the search string and fall within the date range
        i = 1
        For Each objFile In objFolder.Files
            If InStr(1, objFile.Name, strSearch, vbTextCompare) > 0 And _
                objFile.DateLastModified >= Range("D2").Value And _
                objFile.DateLastModified <= Range("E2").Value Then
                Range("C" & i).Value = objFile.Name
                i = i + 1
            End If
        Next objFile
        
        'Clean up the objects
        Set objFile = Nothing
        Set objFolder = Nothing
        Set objFSO = Nothing
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: Macro to list files containing TB within a specified date range in C:\My Documents

    try
    strSearch = "TB"

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to list files containing TB within a specified date range in C:\My Documents

    Thanks for pointing out the problem

    Have amended code and run macro. It now wors 100%

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to move Files from one Folder to another based on Date Range
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-25-2021, 10:52 PM
  2. Replies: 4
    Last Post: 06-02-2019, 02:20 PM
  3. Replies: 7
    Last Post: 09-06-2013, 11:44 AM
  4. convert all 97-2003 excel documents to macro enabled documents on server
    By viperisback in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-23-2013, 09:06 AM
  5. Macro to show date range from 'custom' column (Quarter 1) from list of dates
    By danlRB80 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2013, 09:16 AM
  6. Noob 4 Help - Macro to LIST ALL FILES IN FOLDER and then IMPORT ALL LISTED FILES
    By StlSmiln in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-25-2012, 04:02 AM
  7. Replies: 0
    Last Post: 11-08-2007, 03:39 AM

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