How to List Files in a Folder Without Extensions Using Excel VBA?
If you need to generate a list of files in a specific folder using Excel VBA, but want to exclude the file extensions from the list, there is a simple way to accomplish this. By leveraging the power of VBA in Microsoft Excel, you can easily create a macro that will list all the files in a designated folder while omitting their extensions. This article will provide step-by-step instructions on how to achieve this.
Understanding the VBA Code
To list files in a folder without their extensions using Excel VBA, you’ll need to use a combination of the FileSystemObject
and Dir
functions. Here’s an overview of the key components in the VBA code:
FileSystemObject
: This object allows you to work with drives, folders, and files.Dir
function: This function returns a String representing the name of a file, directory, or folder that matches a specified pattern or file attribute.Left
andLen
functions: These functions are used to extract the file name without the extension.
Step-by-Step Guide to List Files in a Folder Without Extensions Using Excel VBA
Follow these steps to create an Excel VBA macro that lists files in a folder without their extensions:
Step 1: Open Visual Basic Editor
- Open Microsoft Excel.
- Press
Alt+F11
to open the Visual Basic Editor (VBE).
Step 2: Insert a New Module
- In the VBE, go to
Insert
>Module
to insert a new module.
Step 3: Enter the VBA Code
Copy and paste the following code into the new module:
Sub ListFilesWithoutExtensions()
Dim folderPath As String
Dim fileName As String
Dim rowNum As Integer
Dim objFSO As Object
folderPath = "C:\Your\Folder\Path" ' Replace with your folder path
rowNum = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
fileName = Dir(folderPath & "\*")
Do While fileName <> ""
Cells(rowNum, 1) = Left(fileName, Len(fileName) - 4)
rowNum = rowNum + 1
fileName = Dir()
Loop
Set objFSO = Nothing
End Sub
Make sure to replace "C:\Your\Folder\Path"
with the actual path of the folder containing the files you want to list.
Step 4: Run the Macro
- Close the VBE and return to the Excel worksheet.
- Go to the
Developer
tab and click onMacros
. - Select the
ListFilesWithoutExtensions
macro and clickRun
.
The macro will now list all the files in the specified folder without their extensions in the first column of the active worksheet.
Breaking Down the VBA Code
Let’s take a closer look at each part of the VBA code to understand how it works:
Declaring Variables
Dim folderPath As String
Dim fileName As String
Dim rowNum As Integer
Dim objFSO As Object
These lines declare the variables that will be used in the code:
folderPath
stores the path of the folder containing the files.fileName
holds the name of each file in the folder.rowNum
keeps track of the current row number in the Excel worksheet.objFSO
is an object of typeFileSystemObject
.
Setting the Folder Path and Initialize Row Number
folderPath = "C:\Your\Folder\Path" ' Replace with your folder path
rowNum = 1
Here, you need to replace "C:\Your\Folder\Path"
with the actual path of the folder you want to list files from. The rowNum
variable is initialized to 1, indicating that the file names will be listed starting from the first row of the worksheet.
Creating FileSystemObject and Getting First File Name
Set objFSO = CreateObject("Scripting.FileSystemObject")
fileName = Dir(folderPath & "\*")
These lines create an instance of the FileSystemObject
and assign it to the objFSO
variable. The Dir
function is then used to retrieve the first file name in the specified folder. The "\*"
pattern is used to match all files in the folder.
Looping Through Files and Listing Names Without Extensions
Do While fileName <> ""
Cells(rowNum, 1) = Left(fileName, Len(fileName) - 4)
rowNum = rowNum + 1
fileName = Dir()
Loop
This Do While
loop continues to iterate as long as there are file names to process. Inside the loop:
- The
Left
function is used to extract the file name without the extension. It takes thefileName
and subtracts 4 from its length to remove the last four characters (assuming the extension is three characters long, plus the dot). - The extracted file name is then written to the corresponding cell in the first column of the worksheet, using
Cells(rowNum, 1)
. - The
rowNum
is incremented by 1 to move to the next row. - The
Dir
function is called again (without arguments) to retrieve the next file name.
Cleaning Up
Set objFSO = Nothing
End Sub
After the loop ends, the FileSystemObject
is set to Nothing
to free up system resources. The End Sub
statement marks the end of the macro.
Handling Different Scenarios
Files with Multiple Periods in the Name
If your files have multiple periods in their names (e.g., file.name.ext
), you can modify the code to handle this scenario. Instead of subtracting 4 from the length of the file name, you can use the InStrRev
function to find the position of the last period and then use Left
to extract the file name up to that position:
Cells(rowNum, 1) = Left(fileName, InStrRev(fileName, ".") - 1)
Files Without Extensions
If some of the files in the folder don’t have extensions, the current code will include the last four characters of the file name as part of the listed name. To handle this situation, you can add an additional check using the InStr
function to determine if the file name contains a period. If it doesn’t, the entire file name will be listed without modification:
If InStr(fileName, ".") > 0 Then
Cells(rowNum, 1) = Left(fileName, Len(fileName) - 4)
Else
Cells(rowNum, 1) = fileName
End If
Final Thoughts
By using Excel VBA and the FileSystemObject
, you can easily create a macro that lists files in a folder without their extensions. This technique can be helpful when you need to generate a clean list of file names for various purposes. With a few modifications, you can adapt the code to handle different file naming scenarios and ensure that your list accurately reflects the desired output.
Remember to replace the folder path in the code with the actual path of the folder you want to list files from. Once you’ve set up the macro, you can run it whenever you need to update the file list, saving you time and effort in manually removing extensions.
FAQs
What is the purpose of listing files in a folder without extensions using Excel VBA?
What Excel version is required to run the VBA macro?
How do I modify the code to list files from a specific folder?
Can the macro handle files with multiple periods in their names?
How does the macro handle files without extensions?
Can I run this macro on a Mac version of Excel?

Vaishvi Desai is the founder of Excelsamurai and a passionate Excel enthusiast with years of experience in data analysis and spreadsheet management. With a mission to help others harness the power of Excel, Vaishvi shares her expertise through concise, easy-to-follow tutorials on shortcuts, formulas, Pivot Tables, and VBA.