Unable to get the file names in a folder.

Takae

Well-known Member
Joined
Jul 1, 2015
Messages
791
Platform
  1. Windows
Hello everyone,

These codes below are very basic code for getting the filenames in a folder. However, only one user of my teams can't get the filenames.

When the user executes the test1 code, the macro gets the path, but can't get the filenames. (No error)
When the user executes the FSO code, the macro can't even get the path and displays Err76.

Any help would be greatly appreciated.

VBA Code:
Sub test1()
    Dim buf As String, cnt As Long
    Dim path As String
    path = Environ("UserProfile") & "\Box\test\"
    buf = Dir(path)
    Do While buf <> ""
        cnt = cnt + 1
        Sheets("sheet1").Cells(cnt + 9, 2) = buf
        buf = Dir()
    Loop
End Sub

VBA Code:
Sub FSO()
    Dim FSO As Object
    Dim Fld, Fl, F
    Dim i As Long
    Dim path As String
    path = Environ("UserProfile") & "\Box\test\"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Fld = FSO.GetFolder(path)
    Set Fl = Fld.Files
    i = 10
    For Each F In Fl
        Cells(i, 3).Value = F.Name
        i = i + 1
    Next
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Have been using this one for years
VBA Code:
Sub GetFileNames()

    Dim xRow As Long
    Dim xDirect$, xFname$, InitialFoldr$

    InitialFoldr$ = "G:\"    '<<< Startup folder to begin searching from

    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
                ActiveCell.Offset(xRow) = xFname$
                xRow = xRow + 1
                xFname$ = Dir
            Loop
         End If
    End With
End Sub
 
Upvote 0
Hello Kerryx,
I tried it, but the file name was not displayed only with the folder name.
Either way, your code also uses the Dir function, so I would expect the same result to be returned if run by the user mentioned above.
Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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