Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Retrieving Drive Statistics.

Retrieving Drive Statistics

Written by Allen Wyatt (last updated May 4, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003


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:

  1. Choose the References option from the Tools menu. VBA displays the References dialog box.
  2. In the list of available references, make sure Microsoft Scripting Runtime is selected.
  3. Click on OK.

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.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2716) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Retrieving Drive Statistics.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing a List of AutoCorrect Entries

Want a printed record of the AutoCorrect entries you've created in Word? There is no built-in way to do it, but you can ...

Discover More

Hiding Errors

If you find the green and red squiggly underlines that Word adds to your document distracting, you might want a quick way ...

Discover More

Adding Footnotes to Endnotes

Word does footnotes. Word does endnotes. Word doesn't do footnotes within endnotes. Here's a discussion as to why and ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Using Named Ranges in a Macro

Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...

Discover More

Continuing Macro Lines

Sometimes a macro command line can get very, very long. This can make it hard to understand when you look at it a month ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.