
Tips.Net > ExcelTips Home > Macros > Creating Macros > Retrieving Drive Statistics
Summary: Need to gather some information about the drives on a system? It can be pretty easy to do using a macro, as shown in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
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 removeable media—such as floppy drives—they may not show up unless you have media in them.)
Tip #2716 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
Check out ExcelTips Premium today!
You can put times into a worksheet, but then what? Need to do calculations with times? How about working with elapsed time? Don't be confused; learn how easy it can be. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Bugs and Pests Tips
ExcelTips
Family Tips
Health Tips
Home Tips
Organizing Tips
WordTips
Advertise on the
ExcelTips Site