Where can I embedd the address of return results for this VBA to get file names?

outlawdevil

Board Regular
Joined
Jun 30, 2009
Messages
238
I want to input a code where I can specify where it can return the For Next loop. CUrrently it is in Cell A1. THanks.
VBA Code:
Sub LoopThroughFiles ()

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\VBA Folder")

For Each oFile In oFolder.Files

    Cells(i + 1, 1) = oFile.Name

    i = i + 1

Next oFile

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How about
VBA Code:
Sub LoopThroughFiles()

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Dim Loc As String

Loc = InputBox("Please enter start point in the form ""A1""")
If Loc = "" Then Exit Sub
If Not Evaluate("isref(" & Loc & ")") Then Exit Sub
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\mrexcel")

For Each oFile In oFolder.Files

    Range(Loc).Offset(i) = oFile.Name

    i = i + 1

Next oFile

End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub LoopThroughFiles()

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Dim Loc As String

Loc = InputBox("Please enter start point in the form ""A1""")
If Loc = "" Then Exit Sub
If Not Evaluate("isref(" & Loc & ")") Then Exit Sub
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\mrexcel")

For Each oFile In oFolder.Files

    Range(Loc).Offset(i) = oFile.Name

    i = i + 1

Next oFile

End Sub
Thanks Fluff!I just changed Loc declaration to my cell value.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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