The runtime error 63 in VBA—*"Bad Record Number"*—occurs when your macro tries to read or write data to an Excel file that has a corrupted record structure, often from improper formatting, incomplete saves, or clashing data types. It’s a safety net VBA uses when it detects that the file’s binary data is incompatible with what the macro expects. Here’s how to fix it, from simplest checks to deeper diagnostics.
What You'll Need
- A working copy of Excel (2016 or later, or VBA-enabled versions like 2010–2013).
- Access to the Excel file containing the problematic macro.
- If applicable, a backup of the file before making changes.
- VBA editor (Excel’s built-in editor, or Visual Studio Community for advanced scenarios).
Step-by-Step
- Check the file for corruption. Open the Excel file in question. If it’s an older version (pre-2010), try opening it in a newer version—some corruption is version-specific. If the file opens with unsaved changes or artifacts, close it, reopen it, and try running the macro again. If the error persists, proceed to the next steps.
-
Verify the record number in the macro.
Open the VBA editor by pressing
Alt + F11. Navigate to the module where your macro is defined. Look for any calls toWorkbooks.Open,Worksheets.Open, orRange.Readthat might reference a record number. The error often stems from a misaligned record pointer (e.g., using a record number that exceeds the file’s actual data range or is negative). Fix: If you’re reading from a fixed record (e.g., usingRecordset.Openor a binary format), ensure the record number is within bounds. For example:Dim recordNum As Long: recordNum = 1 ' Must match the file's actual record count Set rs = Workbook.Range("Data").Read(Recordset:=True, RecordNum:=recordNum) -
Repair the file’s data structure.
If the error occurs when writing data, the issue may be in how the file is formatted. Try:
- Converting the file to a different format (e.g., CSV) and re-importing it into Excel. This bypasses VBA’s binary expectations.
- Using
Range.Copy+Worksheets.Pasteto manually re-enter data instead of writing directly to the file.
-
Update the macro to handle errors gracefully.
Wrap the problematic code in error handling to avoid crashing:
On Error Resume Next ' Skip error and continue ' Your macro code here If Err.Number <> 0 Then MsgBox "Error: " & Err.Description & vbCrLf & "Record number may be invalid." Err.Clear End If On Error GoTo 0If that did not work: Add this before the record operation:Err.Clear Set rs = Nothing ' Clear any existing recordsetThis resets the object before attempting to read/write. -
Repair the Excel file itself.
If the file is corrupted, use Excel’s built-in repair tools:
- Go to File > Open, then click the Open and Repair button.
- Choose Repair File and retry opening it. If the macro still fails, try opening the file in a different version of Excel.
- Recompile the macro with fresh dependencies. If the error persists, the issue might be with the macro itself (e.g., a hardcoded record pointer or an outdated library). Delete the macro module, reopen it, and re-enter the code. For external libraries (e.g., ADODB), ensure you’re linking to the correct DLLs in the VBA project settings.
Common Mistakes
- Assuming record numbers are sequential. Files often skip records (e.g., due to deleted rows or merged cells). Test the macro with a smaller dataset to isolate the problematic record.
- Ignoring file version compatibility. Macros written for older Excel versions (e.g., 2003) may fail on newer files due to changes in binary formats. Test in the target Excel version.
- Overwriting data without checking interoperability. If the macro edits the same file where it’s reading from, record numbers may conflict. Use temporary files or separate worksheets.
- Using deprecated VBA functions. Replace old methods like
Workbooks.Openwith modern alternatives likeApplication.Workbooks.AddorWorksheet.OpenXMLfor better compatibility.
Tips
- Monitor the
Err.Descriptionvariable in the macro to identify which record operation failed:Debug.Print "Error: " & Err.Description - For large files, use
Range.WriteorWorksheet.Pasteinstead of binary formats to avoid corruption. - If the file is shared, notify collaborators to avoid concurrent writes that break records.
- Use tools like
xlrd(Python library) to inspect a file’s binary structure if VBA debugging is insufficient.
Troubleshooting
- If the error appears only in certain files. Test the macro on a simple, known-good file first. If it works there, compare the problematic file’s structure (e.g., number of rows, data types) to the working file. Mismatches often cause "bad record" errors.
-
If the macro runs but fails silently.
Add
On Error Resume Nextbefore the record operation and check for silent errors in the Immediate Window (Debug > Windows > Immediate) for clues. - If the file won’t open at all. Use Excel’s File > Open > Browse and select the file, then hit Alt + Enter to force a binary open. This bypasses some corruption checks.
-
If you’re working in a team.
Avoid using
Workbooks.Openin shared files—it can cause conflicts. Instead, useApplication.Workbooks.Addto create a new file with the macro. - If the issue persists after all steps. Create a minimal test file with just one record and the macro. Gradually add complexity until the error reappears. This isolates the problematic component.
Frequently Asked Questions
What causes VBA's "Bad Record Number" error in Excel?
The error typically arises from mismatched record sizes, missing data, or improperly formatted binary files. It can also occur if the macro assumes a certain record layout (e.g., fixed-width columns) but the file’s actual data doesn’t match.
Can I fix a corrupted Excel file without losing data?
Yes. Use Excel’s Open and Repair feature (File > Open > Open and Repair) to attempt recovery. For deeper corruption, manually re-enter data into a fresh file. Avoid opening the file in read-only mode if possible—corruption is often worse there.
How do I check if a macro is reading the correct record number?
Add this debug line before the record operation:
Debug.Print "Record range: 1 To " & Worksheet.Range("A1").End(xlDown).Row
Compare it to your macro’s RecordNum value. If they don’t match, adjust the macro.
What’s the difference between a record number and a row number?
A record number refers to a discrete data unit (e.g., a single row in a binary file or a structured block in a CSV). A row number is Excel’s sequential label. In VBA, records align with rows but may skip (e.g., if rows are merged or hidden).
Can I use a different file format to avoid this error?
Yes. Convert the file to CSV or XML. These formats don’t rely on binary records, so macros using Worksheet.Paste or Range.Copy will work without corruption.
Why does my macro work in one file but not another?
The files likely have different data structures. For example:
- The first file has 100 rows with consistent record sizes.
- The second file has irregular data (e.g., empty rows, merged cells) that break the macro’s record assumptions.