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.

Retrieving Drive Statistics

Written by Allen Wyatt (last updated March 8, 2023)
This tip applies to Excel 2007 and 2010


10

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

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

Deleting Commented Text

Word allows you to add comments to your document to aid in the development of the document. At some point you may want to ...

Discover More

Table Header Rows after a Manual Page Break

Insert a manual page break into the middle of a table, and you may find that subsequent pages of the table don't always ...

Discover More

Creating a Function Inventory for a Workbook

Your worksheets are very often made up of formulas and these formulas are made up of functions. If you ever want to ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Converting Numbers to Strings

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

Seeing the Difference on the Status Bar

Do you often need to know the difference between two values in your worksheet? This tip shares a quick little macro that ...

Discover More

Selecting a Range of Cells Relative to the Current Cell

When processing information in a macro, you often need to select different cells relative to the currently selected ...

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 three less than 3?

2023-03-09 11:25:34

J. Woolley

@Alan L
Not all that. But you can return TotalSize (bytes) and DriveType (Unknown, Removable, Fixed, Network, CD-ROM, RAM Disk). My SSD is called Fixed, not RAM Disk. See https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/drive-object


2023-03-08 22:21:07

Alan L

Is there any other information that can be pulled? For instance Drive Make, Model, Size (in Gb or Tb), Physical size (3.5 in, 2.5), Speed (5400), SSD, etc.


2019-02-14 08:05:44

Ken Varley

"Dim Drv As Drive" failed with "User defined type not defined"

Presumably some other instruction needs to be added to define the DRIVE type


2018-12-01 12:11:48

Willy Vanhaelen

@Paul
On one hard disk you can have several partitions and they have each their own drive letter and they each have their used and unused percentages even if each drive has it's own entire physical disk. I have no experience with servers but I suppose that it will be the same.


2018-11-30 15:34:16

Paul

Ignore my last question. I just realized that most of the "drives" I have are all on one server and obviously they are all the same amount of free space, since it is on the same hard drive.


2018-11-30 15:27:35

Paul

Great for Drive letters! Thanks! How easy would it be to change the macro to do the root folders on a server (even the ones you are not naturally assigned a drive letter to)?


2014-12-03 22:05:10

Locke Garmin

You're welcome! I remember how excited I was too when I realized it could be that simple. :)


2014-11-28 02:22:05

Dave K

@ Locke Garmin:

Thank you SO MUCH for that tip! Up to now I have been using one line of code per cell to assign column headers.

Not any more!!!


2014-11-27 09:30:47

Locke Garmin

One thing I like to do is add header columns in the macro in this way:

Range("A1:C1") = Array("Drive", "Free%", "Used%")


2014-11-27 08:34:37

balthamossa2b

Very nice tip.

I love how powerful FSOs are, you can pretty much do anything with them.


This Site

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.

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.