Get list of file names in folder/directory with Excel VBA

Kelsius

I have the following code which pulls the file names from the directory I specify. I found it on the internet and modified it to work for what I need.

The problem is that I don't want it to popup with a window asking me to pick a folder - I want to use the specified folder. How can I change this code so that I don't have to use the window, or if I can't change it, what can I do about my situation?

Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = "C:\Desktop" '<<< 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) = Left(xFname$, InStrRev(xFname$, ".") - 1)
            xRow = xRow + 1
            xFname$ = Dir
        Loop
    End If
End With
Kelsius

I ended up changing my code completely and not using the old code. Again, I found some code on the internet and modified it to work for what I need.

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim FileArray() As Variant
Dim FileCount As Integer
Dim FileName As String
Dim rng As Range
Dim Idx As Integer

FileCount = 0
FileName = Dir("C:\Desktop")

'   Loop until no more matching files are found
Do While FileName <> ""
    FileCount = FileCount + 1
    ReDim Preserve FileArray(1 To FileCount)
    FileArray(FileCount) = FileName
    FileName = Dir()
Loop
GetFileList = FileArray
Set rng = ActiveCell
For Idx = 0 To FileCount - 1
    ActiveCell.Offset(Idx, 0).Value = Left(FileArray(Idx + 1), InStrRev(FileArray(Idx + 1), ".") - 1)
Next Idx

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel VBA efficient get file names function

From Dev

Creating a list/array in excel using VBA to get a list of unique names in a column

From Dev

Get skipped file names into Excel sheet

From Dev

Get all sheet names in an Excel file in order

From Dev

Python libtorrent, get file list names

From Dev

How to replace numbers in cells with names from a list in a loop in Excel VBA

From Dev

How to Change the File Names of Selected Rows using Excel VBA

From Dev

Get list of Excel files in a folder using VBA

From Dev

How to extract Excel file header names into array list using POI?

From Dev

Linq to Excel, get column names from CSV file

From Dev

Excel File Names not Displayed

From Dev

Get a list of file names from HDFS using python

From Dev

get animal names from text file into List Of String

From Dev

How to get list of file names from a private remote ip

From Dev

get list of file names and store them in array on linux using C

From Dev

How to get list of file names and their sentence containing a specific search string?

From Dev

Get the id of the selected value in a dropdown validation list on Excel in VBA

From Dev

Bat file list file names

From Dev

File Revision List File Names?

From Dev

excel vba get name of the file from which the button was pressed on the toolbar

From Dev

Excel vba list issue

From Dev

Countifs in Excel with list of names as criteria

From Dev

Batch Files: List file names and folder names

From Dev

Creating list of Occurences from list of names in Excel

From Dev

Creating list of Occurences from list of names in Excel

From Dev

In R: How do I get the column names of a CSV file as a list and values as a list of lists

From Dev

Sending Emails from Excel VBA - Names Not Recognized

From Dev

Adding Sheet Names to Array in Excel VBA

From Dev

How to select all names in Excel with VBA

Related Related

  1. 1

    Excel VBA efficient get file names function

  2. 2

    Creating a list/array in excel using VBA to get a list of unique names in a column

  3. 3

    Get skipped file names into Excel sheet

  4. 4

    Get all sheet names in an Excel file in order

  5. 5

    Python libtorrent, get file list names

  6. 6

    How to replace numbers in cells with names from a list in a loop in Excel VBA

  7. 7

    How to Change the File Names of Selected Rows using Excel VBA

  8. 8

    Get list of Excel files in a folder using VBA

  9. 9

    How to extract Excel file header names into array list using POI?

  10. 10

    Linq to Excel, get column names from CSV file

  11. 11

    Excel File Names not Displayed

  12. 12

    Get a list of file names from HDFS using python

  13. 13

    get animal names from text file into List Of String

  14. 14

    How to get list of file names from a private remote ip

  15. 15

    get list of file names and store them in array on linux using C

  16. 16

    How to get list of file names and their sentence containing a specific search string?

  17. 17

    Get the id of the selected value in a dropdown validation list on Excel in VBA

  18. 18

    Bat file list file names

  19. 19

    File Revision List File Names?

  20. 20

    excel vba get name of the file from which the button was pressed on the toolbar

  21. 21

    Excel vba list issue

  22. 22

    Countifs in Excel with list of names as criteria

  23. 23

    Batch Files: List file names and folder names

  24. 24

    Creating list of Occurences from list of names in Excel

  25. 25

    Creating list of Occurences from list of names in Excel

  26. 26

    In R: How do I get the column names of a CSV file as a list and values as a list of lists

  27. 27

    Sending Emails from Excel VBA - Names Not Recognized

  28. 28

    Adding Sheet Names to Array in Excel VBA

  29. 29

    How to select all names in Excel with VBA

HotTag

Archive