
This means that now I can use all the methods of FileSystemObject to work with files and folders.
#Best excel vba tutorial 2018 code#
Once this is created, you can use it in VBA.īelow is the code that will set the object variable MyFSO as a FileSystemObject object: Sub CreatingFSO() Once you have set the reference to the Scripting FileSystemObject library, you need to create an instance of the FSO object in your code. Creating an Instance of FileSystemObject in the Code The above steps would now allow you to refer to the FSO objects from Excel VBA.

In the References dialog box that opens, scroll through the available references and check the ‘Microsoft Scripting Runtime’ option.Once this is done, you can refer to the files/folders/drives object from within the Excel VBA (just like you can refer the cells, worksheets or workbooks).īelow are the steps to create a reference to the Microsoft Scripting Runtime Library:

When you create a reference to the Scripting Runtime Library, you allow Excel VBA the access to all the properties and methods of files and folder. Setting the Reference to the Microsoft Scripting Runtime Library I will be focussing majorly on the FileSystemObject in this tutorial. This includes the FileSystemObject, Drive, Files, Folders, etc. Note: When you enable FileSystemObject, you can access all the objects in it.
#Best excel vba tutorial 2018 how to#
While both these methods work (and I’ll show you how to do this next), I recommend using the first method.

Since we are dealing with files and folders that are outside of the Excel application, we need to first create a reference to the library that holds these objects (drives, files, folders). Enabling FileSystemObject in Excel VBAįileSystemObject is not available by default in the Excel VBA. Just for the reference purpose, I have covered all the FileSystemObject methods (for each object) at the end of this tutorial. You will get a much better understanding when you go through the examples that I have covered in this tutorial. Similarly, if you want to copy a file, you will use the CopyFile method of the File object.ĭon’t worry if this seems overwhelming or hard to understand. To give you an example, if you want to delete a folder, you will use the DeleteFolder method of the Folder object. TextStream object allows you to create or read text files.Įach of the above objects has methods that you can use to work with these. For example, you can create, open, copy, move, and delete files using this object. For example, you can create, delete, rename, copy folders using this object.įile Object allows you to work with files in your system. What All Objects Can You Access Through FileSystemObject?Īs I mentioned above, you can access and modify files and folders using the FileSystemObject in VBA.īelow is a table that shows the most important objects that you can access and modify using FSO: Objectĭrive Object allows you to get information about the drive such as whether it exists or not, it’s path name, drive type (removable or fixed), it’s size, etc.įolder object allows you to create or modify folders in your system. Note: FSO can only be used in Excel 2000 and later versions. FileSystemObject makes it easy to work with files and folders while keeping the code clean and short. While some of the things mentioned above can also be done using traditional VBA functions (such as the DIR function) and methods, that would lead to longer and more complicated codes. I will cover all these above examples (plus more) later in this tutorial.

