List the files in the folder

mmn1000

Board Regular
Joined
Mar 17, 2020
Messages
76
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi,
I list the files in the data folder using the code below

VBA Code:
Sub listoffile()
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer
directory = "D:\Data\"
fileName = Dir(directory & "*.*")
Do While fileName <> ""
    i = i + 1
    Cells(i, 6) = fileName
    fileName = Dir()
Loop
End Sub

In this code, the list of my files starts from cell F1, for example
I want it to start listing from cell F8 instead of F1

i = i + 1
Cells(i, 6) = fileName
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
Sub listoffile()
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer
directory = "D:\Data\"
fileName = Dir(directory & "*.*")
i = 7    '<---- Insert this line here
Do While fileName <> ""
Rest of code
 
Upvote 0
Code:
Sub This_Might_Be_Faster()
    Dim filelist() As String, i As Long, fName As String
    fName = Dir("D:\Data" & "\*.*")
    While fName <> ""
        i = i + 1
        ReDim Preserve filelist(1 To i)
        filelist(i) = fName
        fName = Dir()
    Wend
    Cells(8, 6).Resize(UBound(filelist)) = Application.Transpose(filelist)
End Sub
 
Upvote 0
Solution
VBA Code:
Sub listoffile()
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer
directory = "D:\Data\"
fileName = Dir(directory & "*.*")
i=7
Do While fileName <> ""
    i = i + 1
    Cells(i, 2) = fileName
    fileName = Dir()
Loop
End Sub
 
Upvote 0
Code:
Sub This_Might_Be_Faster()
    Dim filelist() As String, i As Long, fName As String
    fName = Dir("D:\Data" & "\*.*")
    While fName <> ""
        i = i + 1
        ReDim Preserve filelist(1 To i)
        filelist(i) = fName
        fName = Dir()
    Wend
    Cells(8, 6).Resize(UBound(filelist)) = Application.Transpose(filelist)
End Sub
Thank you for your beautiful guide, it was great
 
Upvote 0
VBA Code:
Sub listoffile()
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer
directory = "D:\Data\"
fileName = Dir(directory & "*.*")
i=7
Do While fileName <> ""
    i = i + 1
    Cells(i, 2) = fileName
    fileName = Dir()
Loop
End Sub
Thank you
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

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