To get data from a Visual Basic (VB or VBA) program into Excel, the most reliable method is to use the Excel object library from within your VB project: add a reference to Microsoft Excel Object Library, create an Excel.Application object, open or add a workbook, write values to cells or a range, then save and close. This works from VB6, VB.NET and VBA. The steps below use VB.NET with the Excel interop library, which is the common modern case; the same objects exist in VB6 with slightly different syntax. You need Excel installed on the same machine, and for VB.NET you need the Microsoft.Office.Interop.Excel reference available through NuGet or the Office installation.

What You'll Need

  • A Windows PC with Microsoft Excel installed (the interop method drives the installed copy of Excel).
  • Visual Studio or another VB.NET IDE, or the VB6 IDE for legacy projects.
  • For VB.NET: the Microsoft.Office.Interop.Excel package added to your project (References > Manage NuGet Packages, or Add Reference > COM > Microsoft Excel Object Library).
  • A folder where the workbook can be saved, with write permission.
  • Sample data in your program to export (a DataTable, an array or a list).

Step-by-Step

  1. Add the Excel reference. In Visual Studio, right-click your project, choose Add > Reference (or Manage NuGet Packages), and add Microsoft.Office.Interop.Excel. In VB6, choose Project > References and tick Microsoft Excel Object Library. Menu labels can differ slightly by Visual Studio version and edition.
  2. Import the namespace. At the top of your code file add Imports Microsoft.Office.Interop.Excel. In VB6 you would instead declare objects with As Excel.Application.
  3. Declare the Excel objects. Create variables for the application, workbook and worksheet: Dim xlApp As New Excel.Application, Dim xlBook As Excel.Workbook, Dim xlSheet As Excel.Worksheet. Set xlApp.Visible = False so Excel runs in the background.
  4. Add a workbook and worksheet. Call xlBook = xlApp.Workbooks.Add() and then xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet). Give the sheet a name with xlSheet.Name = "Export" if you want a predictable tab label.
  5. Write the header row. Set each header cell individually, for example xlSheet.Cells(1, 1).Value = "Name" and xlSheet.Cells(1, 2).Value = "Amount". Individual cell writes are simple and safe for small tables.
  6. Loop through your data and write rows. Keep a row counter starting at 2. For each record, write to xlSheet.Cells(row, 1).Value, xlSheet.Cells(row, 2).Value and so on, then increment the counter. Convert dates and numbers to the correct .NET type before assigning so Excel stores them as numbers rather than text.
  7. Format if needed. Use xlSheet.Range("A1:B1").Font.Bold = True for headers and xlSheet.Columns.AutoFit() to size columns. Keep formatting minimal; every extra call to the Excel object model is slow.
  8. Save the workbook. Call xlBook.SaveAs("C:\Exports\report.xlsx"). Use the full path including the extension. If the file may already exist, either delete it first or set the display-alert property to false before saving.
  9. Close and release everything. Call xlBook.Close(), then xlApp.Quit(), then set each object variable to Nothing. This is essential: an unreleased Excel process keeps running invisibly in Task Manager.
  10. Wrap the whole block in Try/Catch/Finally. Put the Quit and release calls in the Finally block so Excel is closed even when an error is thrown mid-export.

Common Mistakes

The most frequent problem is leaving Excel processes behind. If you do not explicitly quit the application and release every object, each run leaves a hidden EXCEL.EXE process consuming memory. A second common mistake is writing numbers as strings: if you assign a text value, Excel flags the cell with a green triangle and formulas like SUM ignore it. Convert with CDbl or CInt before assigning.

Another frequent error is using a relative path in SaveAs. The working directory of a VB app is not always the folder you expect, so always build an absolute path with Path.Combine. People also forget that SaveAs fails when the target workbook is already open in Excel, and that writing cell by cell in a loop over thousands of rows is extremely slow. Finally, do not assume the interop library is present on a user's machine: it requires Excel to be installed, and it is not redistributable on its own.

Tips

For large exports, write a two-dimensional array to a range in one call instead of looping cell by cell. Build the array in memory, then assign it to xlSheet.Range(startCell, endCell).Value. This can be hundreds of times faster for big tables.

Set xlApp.ScreenUpdating = False and xlApp.DisplayAlerts = False before writing and restore them afterwards to speed up the run and suppress prompts. If you only need a file and not a live Excel session, consider writing CSV or using a library such as ClosedXML or EPPlus, which do not require Excel to be installed and are far quicker for bulk data. Use interop when you specifically need Excel features such as charts, pivot tables or existing templates.

Always test with a small dataset first, and keep a backup of any template workbook your code opens, because SaveAs overwrites the target file.

Troubleshooting

Excel stays open after the program ends. Open Task Manager and end the stray EXCEL.EXE process, then fix the code by releasing every object in the Finally block. Do not use GC.Collect as a substitute for proper release.

"Cannot create ActiveX component" or a missing reference error. Excel is not installed, the interop reference is missing, or the bitness mismatches (a 64-bit build referencing a 32-bit library). Re-add the reference and check the project's target platform.

SaveAs throws an error. The folder may not exist, the file may be open in Excel, or the path may lack write permission. Check each of these in that order.

Numbers appear as text. Assign typed values, not strings, and format the column as Number after writing.

The export is very slow. Switch to array-based range writes and turn off screen updating.

Frequently Asked Questions

Can I export to Excel without Excel installed?

Yes, but not with interop. Use a library such as ClosedXML or EPPlus, or write a CSV file that Excel can open. Interop drives the installed Excel application and will not work on a machine without it.

Does this work in VB6 as well as VB.NET?

Yes. VB6 uses the same Excel object model through early or late binding, with slightly different syntax for creating objects. The steps and object names are otherwise the same.

How do I export a whole DataTable quickly?

Copy the DataTable into a two-dimensional object array, then assign that array to a range covering the same number of rows and columns in one call.

Why does my exported file open with a warning about protected view?

Files created by another program are often opened in Protected View. The user can enable editing; this is an Excel security setting, not a fault in your export.

Can I write to an existing workbook instead of creating a new one?

Yes. Use xlApp.Workbooks.Open(path), target the required worksheet, write your data, then save and close. Make sure the file is not already open in Excel.