» Basic file and folder examples using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Files, Workbook, and Worksheets in VBA
VERSION - All Microsoft Excel Versions
MsgBox "Active station and folder name: " & CurDir
How to change the active station:
ChDrive "F" ' changes to the F-stationHow to change the active folder:
ChDir "F:\My Documents\Private"How to determine if a file exists in a folder:
If Dir("F:\My Documents\My Workbook.xls") <> "" Then
' the file exists, returns "" (empty string) if the file doesn't exist.
How to delete a file :
Kill "F:\My Documents\My Workbook.xls"If you don't specify the station, Excel uses the active station. If you don't specify the folder, Excel uses the active folder. How to create a new folder:
MkDir "NewPrivateFolder" ' creates a new folder in the active folder MkDir "F:\My Documents\NewPrivateFolder" ' creates a new folder in the existing folder F:\My DocumentsHow to delete a folder (folder must be empty):
RmDir "NewPrivateFolder" ' deletes the subfolder NewPrivateFolder in the active folder RmDir "F:\My Documents\NewPrivateFolder" ' deletes the subfolder NewPrivateFolder in the folder F:\My DocumentsHow to copy a file (the file must be closed):
FileCopy "OrgWorkBook.xls", "CopyWorkBook.xls" ' copies OrgWorkBook.xls to CopyWorkBook.xls in the active folder FileCopy "OrgWorkBook.xls", "F:\My Documents\CopyWorkBook.xls" ' copies OrgWorkBook.xls from the active folder to F:\My Documents\CopyWorkBook.xlsHow to move a file (the file must be closed):
OldFilePath = "C:\OldFolder\Filename.xls" ' original file location NewFilePath = "C:\NewFolder\Filename.xls" ' new file location Name OldFilePath As NewFilePath ' move the file
Book Store:
Recommended Books:
- The 22 Immutable Laws of Branding
- Essentials of Accounting and Post Test Booklet 8, Eighth Edition
- Microsoft Excel Version 2002 Step by Step
- H&R Block's Just Plain Smart(tm) Tax Planning Advisor: A year-round approach to lowering your taxes this year, next year and beyond
- Final Accounting: Ambition, Greed and the Fall of Arthur Andersen
- The 22 Immutable Laws of Marketing : Exposed and Explained by the World's Two
No comments have been submitted.


