Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Retrieving Drive Statistics.
by Allen Wyatt
(last updated November 30, 2018)
If you are creating a full-blown application using Excel, you may want to know a bit about the environment in which your application is running. For instance, you might want to know how many drives are attached to the system, what their drive letters are, and how much space they have free.
The following macro will retrieve the requested information. All you need to do is provide the column headings. The macro assumes that you'll have three columns: In cell A1 you should place the heading "Drive," in cell B1 you place the heading "Free%," and in cell C1 you place the heading "Used%." In addition, you should format columns B and C as percentages.
Sub DriveSizes() Dim Drv As Drive Dim fs As New FileSystemObject Dim Letter As String Dim Total As Variant Dim Free As Variant Dim FreePercent As Variant Dim TotalPercent As Variant Dim i As Integer On Error Resume Next i = 2 For Each Drv In fs.drives If Drv.IsReady Then Letter = Drv.DriveLetter Total = Drv.TotalSize Free = Drv.FreeSpace FreePercent = Free / Total TotalPercent = 1 - FreePercent Cells(i, 1).Value = Letter Cells(i, 2).Value = FreePercent Cells(i, 3).Value = TotalPercent i = i + 1 End If Next End Sub
When you first run this macro, you may get an error. If you do, it means that you need to configure your macro to reference the Microsoft Scripting Runtime. Follow these steps from within the VBA Editor:
Now the macro should run just fine, and you will have a fully populated table representing all the drives available on your system. (If your system has drives that use removable media—such as floppy drives—they may not show up unless you have media in them.)
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11214) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Retrieving Drive Statistics.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
When creating macros, it is helpful to know what is going on within the macro itself in case an error crops up. Here's ...Discover More
When creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...Discover More
Got a workbook that has lots and lots of macros associated with it? Here's a way you can get a list of all of those ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.