Copy File Names in Folder starting from 001_ to 010_

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have tried to write code to copy file names in C:\Sales Reports stating from 001_ to 0010_ and pasting this in A1 onwards on sheet "File Names"

I only paste the file names stating from 001_ to 009_ and does not include 0010_


It would be appreciated if someone could amend my code


Code:
 Sub ExtractFileNames()
    Dim folderPath As String
    Dim fileName As String
    Dim ws As Worksheet
    Dim i As Integer
    Dim rowNum As Integer
    
    ' Set the folder path
    folderPath = "C:\Sales Reports\"
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("File names")
    
    ' Clear previous data in column A
    ws.Range("A1:A10").Clear
    
    ' Initialize row number
    rowNum = 1
    
    ' Loop through files and extract names
    For i = 1 To 10
        fileName = Dir(folderPath & Format(i, "000_") & "*.*")
        
        Do While fileName <> ""
            ws.Cells(rowNum, 1).Value = fileName
            rowNum = rowNum + 1
            fileName = Dir
        Loop
    Next i
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I have managed to resolve the issue

Code:
 Sub ExtractFileNames()
    Dim folderPath As String
    Dim fileName As String
    Dim ws As Worksheet
    Dim i As Integer
    Dim rowNum As Integer
    Dim formatString As String
    
    ' Set the folder path
    folderPath = "C:\Sales Reports\"
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("File names")
    
    ' Clear previous data in column A
    ws.Range("A1:A10").Clear
    
    ' Initialize row number
    rowNum = 1
    
    ' Loop through files and extract names
    For i = 1 To 10
        If i = 10 Then
            formatString = Format(i, "000#_")
        Else
            formatString = Format(i, "00#_")
        End If
        
        fileName = Dir(folderPath & formatString & "*.*")
        
        Do While fileName <> ""
            ' Check if the file name matches the pattern
            If Left(fileName, Len(formatString)) = formatString Then
                ws.Cells(rowNum, 1).Value = fileName
                rowNum = rowNum + 1
            End If
            fileName = Dir
        Loop
    Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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