How To Read Excel File In Visual Basic
To read an Excel file in Visual Basic .NET, add an Excel Interop reference, create an Excel application object, open the workbook, select a worksheet and read its cells or ranges. Close the workbook and Excel afterwards. If your application only needs stored data and.
Which licences fit your PC setup?
Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.
-

Office 2024 Pro Plus key
$20.00 AUD
$39.00 -

Windows 11 Pro key
$14.99 AUD
$29.00 -

Windows 11 Home key
$18.88 AUD
$37.00
Choose the right approach for your application
This guide focuses on VB.NET projects in Visual Studio. VBA, which runs inside Office applications, uses different syntax and project setup. For example, VBA uses Set when assigning object references; VB.NET does not. Keep examples from the two environments separate.
Before adding a dependency, decide whether your program needs to control Excel or simply extract data:
- Use Microsoft.Office.Interop.Excel for a Windows desktop application that needs Excel's object model. Excel must be installed on the computer running the application.
- Use a file-reading library when you need to import workbook data without launching Excel. For example, ExcelDataReader supports several workbook formats, including .xls and .xlsx. Check format support and the licence before choosing a library.
- For a web application or background service, choose a library designed to read files directly. Microsoft does not support server-side Office automation.
The steps below use Interop. Its main objects form a hierarchy: Application contains Workbooks, a Workbook contains Worksheets, and a Worksheet exposes cells through Range objects. You can use the Visual Basic guides for supporting language topics as you build the import.
Read a workbook with Excel Interop
Start with a small workbook whose worksheet name, headers and expected values you know. Use an explicit file path and range so that you can check the result before handling variable layouts.
Add the Excel reference. In Visual Studio, add a reference to Microsoft.Office.Interop.Excel, or use the Microsoft Excel Object Library through the COM reference dialog as appropriate for your project. At the top of the source file, add Imports Excel = Microsoft.Office.Interop.Excel. The Imports statement provides a shorter name; it does not add the dependency itself. Microsoft's reference and Imports guidance explains that distinction.
Prepare the file and object variables. Confirm that the path points to an existing workbook and that the application can read it. Declare variables for Excel.Application, Excel.Workbooks, Excel.Workbook, Excel.Sheets, Excel.Worksheet and Excel.Range outside your Try block so that cleanup can access any objects successfully created.
Create an application instance owned by your program. Inside Try, assign excelApp = New Excel.Application(), then books = excelApp.Workbooks. Keeping references in named variables makes their lifetime easier to manage.
Open the workbook for reading. Assign workbook = books.Open(filePath, UpdateLinks:=0, ReadOnly:=True). ReadOnly opens the workbook without requesting editing access; UpdateLinks:=0 prevents external workbook references from updating during opening. These options are documented in Workbooks.Open. Password-protected or damaged files still need separate handling.
Select the worksheet explicitly. Assign sheets = workbook.Worksheets, then worksheet = DirectCast(sheets.Item("Sheet1"), Excel.Worksheet), replacing Sheet1 with the actual tab name. Avoid relying on ActiveSheet: the selected tab may differ between files.
Read a cell or rectangular range. For one value, assign dataRange = worksheet.Range("A1"), followed by rawValue = dataRange.Value2, with rawValue declared as Object. For several rows, use worksheet.Range("A1:C10") instead. A multi-cell rectangle returns a two-dimensional array; a single cell returns a scalar value.
Process the retrieved data and close Excel in Finally. Close the workbook with workbook.Close(SaveChanges:=False), then call excelApp.Quit() for the instance your application created. Check that each object was initialised before using it during cleanup, and arrange cleanup so a failed Close does not prevent attempting Quit.
Read-only access and suppressed link updates are separate from macro security. When handling macro-enabled workbooks, set the intended macro policy before opening them; Microsoft's AutomationSecurity documentation describes its behaviour and limitations.
Read ranges efficiently and interpret the values
Transfer a block of cells before looping
For a rectangular dataset, read its Value2 property once, then loop through the resulting array in VB.NET. This reduces repeated calls between your application and Excel. Microsoft recommends bulk range access in its Excel performance guidance.
For a known multi-cell range, Dim values As Object(,) = DirectCast(dataRange.Value2, Object(,)) stores the result. Use values.GetLowerBound(0) and values.GetUpperBound(0) for row limits, and the corresponding bounds for dimension 1 for columns. Do not assume the returned array starts at zero. Handle a single-cell range separately.
Limit reading to the data you need. UsedRange can include cells affected by old formatting, so it is not a reliable count of business records. Prefer a known range, an Excel table's data area, or a boundary based on a required column. Decide whether headers belong in the imported data.
Validate types before calculating
A blank cell, text value, number and Excel error require different treatment. Do not convert every value directly to a number. Define whether missing values should be skipped, rejected or replaced, and identify invalid records by worksheet and row.
Value2 reads underlying values rather than formatted display text. Dates can arrive as serial numbers, and a numeric identifier displayed with leading zeroes will not retain those zeroes as part of its numeric value. Use the column's expected meaning when converting it, rather than relying on appearance alone.
After validating numeric inputs, you can calculate an average in Visual Basic. If the output needs a fixed decimal presentation, decide whether to format the display or round to two decimal places in the underlying calculation.
Handle failures and check resource cleanup
Catch errors where the application can explain or recover from them. A missing file needs a different response from a missing worksheet or an invalid amount. Include the relevant path, worksheet or cell location in diagnostic messages, and avoid silently importing incomplete data.
Closing a workbook does not remove every COM reference held by .NET. Keep object ownership clear and avoid retaining ranges after the import. Do not apply a generic ReleaseComObject helper indiscriminately: Microsoft warns that releasing a shared COM wrapper can break other code using it.
Next, import a small known range and compare every returned value with the workbook. Repeat with a blank cell, a missing worksheet and an invalid path, checking that your application's Excel instance exits after each attempt.
Other editions for separate workloads
Other editions for separate workloads; prices in AUD, confirm the total on the listing.
-

Windows 10 Pro key
$12.00 AUD
$15.00 -

Microsoft 365 Pro Plus, 5 devices
$15.00 AUD
$29.00 -

Windows Server 2022 Standard
$120.00 AUD
$239.00
How to check this worked
- Open Settings, System, Activation and confirm the edition shown.
- Enter the key and run activation; expect a green tick within a minute.
- Reboot and re-check activation survives the restart.
FAQ
What else should you know before buying?
- What do I need before starting?
- Read the requirements and confirm the edition first; the steps assume the machine is compatible.
- How do I know the licence is genuine?
- Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
- Is this a subscription?
- Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
- What if the key fails?
- Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.
Check who prepared and reviewed this guide
- Written by
- MS.Codes editorial
- Reviewed by
- Support team
Last verified: (source review; see test status above).