VBA & Macro Problems

How to Export Access Data to Excel and Protect Header Row via VBA

Khadija KhanKhadija Khan Oct 16, 2026 869 views

Question details

The user wants to export database query results from Microsoft Access to Microsoft Excel and ensure that the column headers in the resulting spreadsheet are automatically protected from user modifications.

How to Export Access Data to Excel and Protect the Header Row
Product
Microsoft Access and Excel
Device & OS
not provided
Scenario
Automating routine data exports where the generated Excel files must be shared with users, requiring the top header row to be locked to prevent accidental structural changes.
Observed behavior
By default, Access exports query data into an unprotected Excel worksheet. The user needs a programmatic way to incorporate worksheet protection into the existing Access VBA export process.
Before you start

Make sure you have a basic understanding of VBA in both Access and Excel, and verify that the Microsoft Excel Object Library is enabled in your Access VBA references.

Solution 1Recommended

Record an Excel Macro and Adapt it for Access VBA

The most efficient way to obtain the exact VBA syntax for locking cells and protecting a worksheet is to record a macro in Excel, then port and adjust that code within your Access export routine.

Because both Microsoft Access and Microsoft Excel use Visual Basic for Applications (VBA), they can share and execute similar code structures. However, code recorded natively in Excel must be modified slightly to run from Access, as Access needs explicit references to the Excel Application and Workbook objects.

1
Record the protection steps in Excel

Open a sample Excel file. Go to the Developer tab and click 'Record Macro'. Select all cells (Ctrl+A), right-click, choose 'Format Cells', go to the Protection tab, and uncheck 'Locked'. Next, select only Row 1, format the cells again, and check 'Locked'. Finally, go to the Review tab and click 'Protect Sheet'. Stop the macro recording.

2
Extract the generated VBA code

Press ALT + F11 to open the Excel VBA editor. Find the newly created Module in the Project Explorer and copy the generated VBA code.

3
Integrate the code into Access

Open your Microsoft Access database and press ALT + F11 to open the Access VBA Editor. Navigate to the procedure you currently use to export the data and instantiate the Excel application. Paste the copied code near the end of your export routine.

4
Adjust object references

Modify the pasted code so it explicitly references the Excel worksheet object created in your Access VBA. For example, change 'Rows("1:1").Select' and 'Selection.Locked = True' to 'xlSheet.Rows("1:1").Locked = True', and 'ActiveSheet.Protect' to 'xlSheet.Protect' (assuming 'xlSheet' is your worksheet variable).

Record an Excel Macro and Adapt it for Access VBA
Reference Requirement: You must enable the 'Microsoft Excel XX.X Object Library' via Tools > References in the Access VBA editor to seamlessly use Excel objects in your Access code.
Free Microsoft Office alternative

Try WPS Office for Your Spreadsheet and VBA Needs

While Microsoft Access is required for complex relational database tasks, WPS Spreadsheet is an excellent, free, and lightweight alternative to Microsoft Excel for analyzing and formatting your exported data. It features full compatibility with Excel formats and provides robust VBA support for your automated macros.

  1. 1. Download and Install WPS Office: Visit the official WPS website, download the free installer, and complete the lightweight installation process.
  2. 2. Open your exported data: Launch WPS Spreadsheet and easily open the .xlsx or .csv files you exported from your database.
  3. 3. Run your macros: Enable the Developer tab to access the built-in VBA editor, allowing you to run custom macros and protect your header rows seamlessly.
Fully compatible with Microsoft Excel file formats (.xlsx, .xls, .xlsm, .csv).Robust built-in VBA and Macro support to automate your spreadsheet formatting.Lightweight architecture ensures fast performance even with large exported datasets.Free to download with a familiar, easy-to-navigate ribbon interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get an 'Object Required' error when running the Excel macro in Access?

This error typically occurs if you paste the Excel macro into Access without updating the object references. Access VBA does not inherently know what 'ActiveSheet' or 'Selection' means. You must prefix these commands with the explicit Excel Application or Worksheet variable you declared in your Access code.

How do I lock only the header row and leave the rest of the Excel sheet editable?

By default, all cells in a new Excel worksheet have their 'Locked' property set to True. Before protecting the sheet, your VBA code must first unlock all cells (e.g., `xlSheet.Cells.Locked = False`), then specifically lock only the first row (e.g., `xlSheet.Rows(1).Locked = True`), and finally execute the `Protect` method.

Does WPS Spreadsheet support executing VBA to protect worksheets?

Yes, compatible professional versions of WPS Spreadsheet support standard VBA functionality. You can execute standard sheet protection methods like `Worksheet.Protect` to secure specific rows or columns in your documents.