How to Export Access Data to Excel and Protect Header Row via VBA
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.

- 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.
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.
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.
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.
Press ALT + F11 to open the Excel VBA editor. Find the newly created Module in the Project Explorer and copy the generated VBA code.
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.
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).

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. Download and Install WPS Office: Visit the official WPS website, download the free installer, and complete the lightweight installation process.
- 2. Open your exported data: Launch WPS Spreadsheet and easily open the .xlsx or .csv files you exported from your database.
- 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.

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.




