Open excel files in a folder (vba)
This tutorial shows you how to list excel files in a specified folder and create adjacent checkboxes, using vba. The last macro opens selected files.
List files in a folder
The following macro is assigned to the "Refresh list" button. It lists all *.xls, *.xlsx and *.xlsm files in the folder specified in cell B1.
Sub ListFiles()
Dim cell As Range, selcell As Range
Dim Value As String
Dim Folder As Variant, a As Long
ReDim Folders(0)
Set cell = Range("A4")
Set selcell = Selection
Range("A4:B10000").Value = ""
Folderpath = Range("B1").Value
If Right(Folderpath, 1) <> "\" Then
Folderpath = Folderpath & "\"
End If
Value = Dir(Folderpath, &H1F)
Do Until Value = ""
If Value <> "." And Value <> ".." Then
If GetAttr(Folderpath & Value) <> 16 Then
If Right(Value, 4) = ".xls" Or Right(Value, 5) = ".xlsx" Or Right(Value, 5) = ".xlsm" Then
cell.Offset(0, 0).Value = Value
cell.Offset(0, 1).Value = FileLen(Folderpath & Value)
Set cell = cell.Offset(1, 0)
End If
End If
End If
Value = Dir
Loop
Call Addcheckboxes
selcell.Select
End SubAdd checkboxes in column C
Sub Addcheckboxes()
Dim cell, LRow As Single
Dim chkbx As CheckBox
Dim CLeft, CTop, CHeight, CWidth As Double
Application.ScreenUpdating = False
ActiveSheet.CheckBoxes.Delete
LRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For cell = 4 To LRow
If Cells(cell, "A").Value <> "" Then
CLeft = Cells(cell, "C").Left
CTop = Cells(cell, "C").Top
CHeight = Cells(cell, "C").Height
CWidth = Cells(cell, "C").Width
ActiveSheet.CheckBoxes.Add(CLeft, CTop, CWidth, CHeight).Select
With Selection
.Caption = ""
.Value = xlOff
.Display3DShading = False
End With
End If
Next cell
Application.ScreenUpdating = True
End SubOpen selected files
The macro below is assigned to "Open selected files" button. It checks if a checkbox is enabled and opens the excel file.
Sub OpenFiles()
Dim Folderpath As String
Dim cell As Range
Dim r, LRow As Single
Dim CWB As Workbook
Folderpath = Range("B1").Value
Set CWB = ActiveWorkbook
If Right(Folderpath, 1) <> "\" Then
Folderpath = Folderpath & "\"
End If
For Each chkbx In ActiveSheet.CheckBoxes
If chkbx.Value = 1 Then
For r = 1 To Rows.Count
If Cells(r, 1).Top = chkbx.Top Then
Workbooks.Open Filename:=Folderpath & Range("A" & r).Value
Exit For
End If
Next r
CWB.Activate
End If
Next
End SubDownload excel *.xlsm file
List all files in a folder.xlsm
Related posts:
Excel recursive udf: List files in a folder and subfolders
Insert hyperlinks to all files in current folder
Excel vba: Add checkboxes to a sheet (1/2)


















This is a great tool, However any idea on how to make it work for excel for mac 2011?
Run-time error '5': invalid prodedure call or argument on Value = Dir(Folderpath, &H1F)
Cyril,
Try this:
Value = Dir(Folderpath)
Run-time error '68': device unavailable on Value = Dir(Folderpath)
Now could he be related with the way the path is written? No c:\ in mac... Any suggestion?
Cyril,
I found this and Dir seems to work on excel for mac 2011:
http://stackoverflow.com/questions/7294838/loop-though-files-in-a-folder-on-mac-os-x-vba-excel-2011
Oscar, what's this "&H1F" supposed to do?
chrisham,
great question, I am not sure where I got it from.
You can find the attributes here: http://www.techonthenet.com/excel/formulas/dir.php
This is great tool. But in my case, i need to list all xls from folder, and its subfolders. Can you help me with this ?
I love this and will be using it everyday from today, but when I try to open the files I get one only to open and get a error "code execution has been interrupted". Can you help me with this?
ok I found this, it seems that it is an excel bug, you just need to put this code as the first line in your sub and thats it...
But be aware that if you have coded endless loops, and you insert the above code, you can't interrupt it using Esc or Ctrl+Pause/Break!!!!
ScottyG,
I found this answer from Dick Kusleika:
It's most likely a corruption of the p-code that's created when you compile. Some people have had success by choose Clear All Breakpoints from the Debug menu. The rock solid method is to export all of your modules, delete them, then reimport them. This round trip through plain text forces a complete rebuild of the p-code. Google for Rob Bovey's Code Cleaner for a free utility that automates this process.
http://stackoverflow.com/questions/6033953/why-does-vba-stop-if-enablecancelkey-is-not-disabled
Hi, do you know why there is a syntax in the code? Please see my post. thank you
seems like there is a syntax with the &:
If GetAttr(Folderpath & Value) 16 Then
How can I fix?
Also, how can I use the xlm files? They dont appear to be .xls files.
thank you
OK I think it is a copy and paste issue. I see the & changes to & Value but must be careful and if it doesnt change then do it manually.
Where do you put the file path at?
Tony,
Thank you for letting me know about the amp error. I removed it from the code. It happens sometimes when I update a post in wordpress, I don´t know why.
Also, how can I use the xlm files? They dont appear to be .xls files.
The following code also lists *.xlm files:
Sub ListFiles() Dim cell As Range, selcell As Range Dim Value As String Dim Folder As Variant, a As Long ReDim Folders(0) Set cell = Range("A4") Set selcell = Selection Range("A4:B10000").Value = "" Folderpath = Range("B1").Value If Right(Folderpath, 1) <> "\" Then Folderpath = Folderpath & "\" End If Value = Dir(Folderpath, &H1F) Do Until Value = "" If Value <> "." And Value <> ".." Then If GetAttr(Folderpath & Value) <> 16 Then If Right(Value, 4) = ".xls" Or Right(Value, 5) = ".xlsx" Or Right(Value, 5) = ".xlsm" Or Right(Value, 4) = ".xlm" Then cell.Offset(0, 0).Value = Value cell.Offset(0, 1).Value = FileLen(Folderpath & Value) Set cell = cell.Offset(1, 0) End If End If End If Value = Dir Loop Call Addcheckboxes selcell.Select End SubHI Oscar, thanks for the reply about the syntax. Where would I put the path to my folder on the network drive? I am not a programmer and dont know much about VBA so would appreciate a lamens explanation. thanks...
Regarding the xlm files, I thought that the link " List all files in a folder.xlsm" would provide xls files ie sample files?
Tony,
Where would I put the path to my folder on the network drive?
Enter the path in cell B1. See the first picture.
oh I see... thank you. Will circle back if I have any issues. I like your website. thanks...
Everything works just great, thank you very much!
I wonder if it would be possible to add another vba code that can uncheck the eventually checked boxes?
Nic,
I wonder if it would be possible to add another vba code that can uncheck the eventually checked boxes?
Sure!
Sub OpenFiles() Dim Folderpath As String Dim cell As Range Dim r, LRow As Single Dim CWB As Workbook Folderpath = Range("B1").Value Set CWB = ActiveWorkbook If Right(Folderpath, 1) <> "\" Then Folderpath = Folderpath & "\" End If For Each chkbx In ActiveSheet.CheckBoxes If chkbx.Value = 1 Then chkbx.Value = 0 For r = 1 To Rows.Count If Cells(r, 1).Top = chkbx.Top Then Workbooks.Open Filename:=Folderpath & Range("A" & r).Value Exit For End If Next r CWB.Activate End If Next End SubHi Oscar thank you for your replay,
I saw the change you made "chkbx.Value = 0" and I did realize that¨my question was not so complete... Sorry for that, my English is not that good, I'm not a programmer and I'm a simple excel user
I try again
I would like to have a button that clear the checked boxes, if i check more than one. Is that possible? If yes, what is the vba code?
Well thank you for your time and have a great day.
My best
Nic
I found the solution, it was too easy for me lol
I just need to reload the dir to uncheck the boxes. I have about 20 dir and about 150 files in each one.
I learned something today.
Thank you again
Hi,
Firstly this is brilliant and just what I was looking for Thank you
ListFiles and Addcheckboxes run fine.
My only problem is I get an error when running the open files code. Run-time error `1004': Application-defined or object-defined error.
I am just starting out using VBA and have no idea how to correct this error, or even find it for that matter!
I am using Excel 2003
any help would be apreciated
Thanks again,
Mark
Hi,
I have got the code to work!
I was trying to use it to open a .doc word file. It works fine with excel files.
I have ammended to search for and list .doc files
Is there anyway to change this code to also open .doc files?
Thank you!
Mark,
I am happy you got it working!
I was trying to use it to open a .doc word file. It works fine with excel files.
I have ammended to search for and list .doc files
Is there anyway to change this code to also open .doc files?
Yes!
Sub ListFiles() Dim cell, selcell As Range Dim Value As String Dim Folder As Variant, a As Long ReDim Folders(0) Set cell = Range("A4") Set selcell = Selection Range("A4:B10000").Value = "" Folderpath = Range("B1").Value If Right(Folderpath, 1) <> "\" Then Folderpath = Folderpath & "\" End If Value = Dir(Folderpath, &H1F) Do Until Value = "" If Value <> "." And Value <> ".." Then If GetAttr(Folderpath & Value) <> 16 Then If Right(Value, 4) = ".xls" Or Right(Value, 5) = ".xlsx" Or Right(Value, 5) = ".xlsm" Or Right(Value, 5) = ".docx" Or Right(Value, 4) = ".doc" Then cell.Offset(0, 0).Value = Value cell.Offset(0, 1).Value = FileLen(Folderpath & Value) Set cell = cell.Offset(1, 0) End If End If End If Value = Dir Loop Call Addcheckboxes selcell.Select End Sub Sub Addcheckboxes() Dim cell, LRow As Single Dim chkbx As CheckBox Dim CLeft, CTop, CHeight, CWidth As Double Application.ScreenUpdating = False ActiveSheet.CheckBoxes.Delete LRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row For cell = 4 To LRow If Cells(cell, "A").Value <> "" Then CLeft = Cells(cell, "C").Left CTop = Cells(cell, "C").Top CHeight = Cells(cell, "C").Height CWidth = Cells(cell, "C").Width ActiveSheet.CheckBoxes.Add(CLeft, CTop, CWidth, CHeight).Select With Selection .Caption = "" .Value = xlOff .Display3DShading = False End With End If Next cell Application.ScreenUpdating = True End Sub Sub OpenFiles() Dim Folderpath As String Dim cell As Range Dim r, LRow As Single Dim CWB As Workbook Folderpath = Range("B1").Value Set CWB = ActiveWorkbook If Right(Folderpath, 1) <> "\" Then Folderpath = Folderpath & "\" End If Set wordapp = CreateObject("word.Application") For Each chkbx In ActiveSheet.CheckBoxes If chkbx.Value = 1 Then For r = 1 To Rows.Count If Cells(r, 1).Top = chkbx.Top Then If Right(Cells(r, 1), 4) = ".xls" Or Right(Cells(r, 1), 5) = ".xlsx" Or Right(Cells(r, 1), 5) = ".xlsm" Then Workbooks.Open Filename:=Folderpath & Range("A" & r).Value ElseIf Right(Cells(r, 1), 5) = ".docx" Or Right(Cells(r, 1), 4) = ".doc" Then wordapp.documents.Open Folderpath & Cells(r, 1) wordapp.Visible = True End If Exit For End If Next r CWB.Activate End If Next End SubDownload *.xlsm file
List-all-files-in-a-folder-open-doc1.xlsm
Thank you !
This works absolutly perfect !
again, thanks for helping me out !
Mark