VBA to open ONLY excel files in a folder

Tanyaann1995

Board Regular
Joined
Mar 24, 2021
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I want to open only Excel files in a folder and the below is the code which I'm using currently. But, I get an error message: "Compile Error: User defined type not defined" and the "Dim FSO" line gets highlighted.

VBA Code:
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file
Set FSO = CreateObject("Scripting.FileSystemObject")
a = wb.path
Set folder = FSO.GetFolder(a)
For Each file In folder.Files
        If Right(file, 5) = ".xlsx" Then
        Workbooks.Open file
        Else
        MsgBox ("No Datasheets found in Sizing folder")
        End If
    Next file
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You need to do one of the following:
1) Either use late binding:
VBA Code:
Dim FSO As Object, folder As Object, file As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
2) Or use early binding. For it you need to: a) Create reference to Scripting library: Tools -> References -> Microsoft Scripting Runtime and b) change code this way:
VBA Code:
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file
Set FSO = New Scripting.FileSystemObject
 
Upvote 0
Solution
You need to do one of the following:
1) Either use late binding:
VBA Code:
Dim FSO As Object, folder As Object, file As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
2) Or use early binding. For it you need to: a) Create reference to Scripting library: Tools -> References -> Microsoft Scripting Runtime and b) change code this way:
VBA Code:
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file
Set FSO = New Scripting.FileSystemObject
Thanks, this worked perfectly. :)
 
Upvote 0
Glad it helped! ? Haven't been here for a while! ?
 
Upvote 0

Forum statistics

Threads
1,216,816
Messages
6,132,867
Members
449,761
Latest member
AUSSW

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