File List skipping First File

DMROD

New Member
Joined
Jul 23, 2010
Messages
6
The following code return a list of files & file path in the directory except for the frist file in directory, but it adds to the end of the list one file path without file. Total lines in list equals number of Files in directory. There is a sub directory in the directory which I do not need in the list nor it contents. What I am missing?

Thank you
David


strDir = ThisWorkbook.Path

Let strName = Dir(strDir & "\*.*")
Do While strName <> vbNullString
Let j = j + 1
' Let strArr(i, 1) = strDir & "\" & strName
Let strName = Dir()
'Sheets("List").Cells(j, 1) = strArr
Sheets("List").Cells(j, 1) = strDir & "\" & strName
Loop
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
David

Are you sure the index of the file list starts at 1?

Perhaps you should move j=j+1?
 
Upvote 0
Norie

I inserted code before the loop to set j = 0 prior to starting the loop. And tried moving the j = j+1 line within the loop. With no success. Any other suggestions.

Thank you
David

Code:
strDir = ThisWorkbook.Path
    Let j = 0
    Let strName = Dir$(strDir & "\*.*")
    Do While strName <> vbNullString
        Let j = j + 1
 
        Let strName = Dir$()
 
        Sheets("List").Cells(j, 1) = strDir & "\" & strName
        Loop
 
Upvote 0
Oops, my bad - I misread your code.

It thought you were using another method to get the fiile list, not just Dir.

I think what you need to do is move the line of code that gets the next filename after the line that puts the current filename on the worksheet.

Your very first Dir should get the first filename.

Code:
    strDir = ThisWorkbook.Path
    
    strName = Dir$(strDir & "\*.*") ' get first filename - if there is one    
    
    Do While strName <> vbNullString
 
        j = j + 1
 
        Sheets("List").Cells(j, 1) = strDir & "\" & strName
 
        strName = Dir$() ' get next filename
        
    Loop
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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