VBA & Macro Problems

Fix VBA Run-Time Error 13 (Type Mismatch) in Excel COUNTA

Anum MushtaqAnum Mushtaq Oct 26, 2026 868 views

Question details

The user is encountering a Run-time error 13 (Type mismatch) when running an Excel VBA script that uses the COUNTA function to find the next available row.

Fix VBA Run-Time Error 13 in Excel COUNTA Code
Product
Microsoft Excel
Device & OS
not provided
Scenario
Using a VBA procedure to calculate the next empty row in a database worksheet so that new data can be appended dynamically.
Observed behavior
Instead of returning the next row number, the macro stops execution and throws a 'Run-time error 13: Type mismatch' message.
Before you start

Verify that your target worksheet name in the workbook exactly matches the sheet name referenced in your VBA script (e.g., 'Database'), as an incorrect or missing sheet name is a common trigger for reference evaluation errors.

Solution 1

Correct the COUNTA Syntax with Bracket Evaluation

Fix the syntax error causing the type mismatch by ensuring you have a complete column reference and a closing parenthesis inside the bracket evaluate method.

When using the shorthand bracket evaluation `[]` in VBA to execute worksheet functions, any missing characters or incomplete references will cause a Type Mismatch error because VBA fails to evaluate the expression as a valid number.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications Editor, and locate the module containing your script.

2
Locate the faulty variable assignment

Find the line of code where you assign the row number using COUNTA. It is likely missing a closing parenthesis or has an incomplete column reference.

3
Apply the correct syntax

Change the line of code to: `iRow = [COUNTA(Database!A:A)] + 1`. Make sure 'Database' matches your actual worksheet name.

4
Run the macro

Press F5 or run your procedure again to confirm that the Run-time error 13 is resolved and the row calculates properly.

Write and Edit Macros in WPS Spreadsheet

Resolve VBA Script Errors with WPS Spreadsheet

WPS Spreadsheet provides excellent support for VBA macros and scripts. You can smoothly run, debug, and edit your existing Excel macros using its built-in Developer environment while avoiding compatibility issues.

  1. 1. Open your macro workbook: Launch WPS Spreadsheet and open the file containing your script.
  2. 2. Access the Visual Basic Editor: Click on the 'Developer' tab in the top ribbon and select 'Visual Basic' to view your code.
  3. 3. Apply the robust code fix: Locate the COUNTA error and update it to the `End(xlUp)` method to ensure your macro runs flawlessly.
Fully compatible with Microsoft Excel macro-enabled files (.xlsm) and VBA syntax.Includes an integrated Visual Basic Editor to easily troubleshoot runtime errors.Lightweight, fast, and handles heavy data operations efficiently.Offers a free comprehensive office suite with a familiar user interface.
QA img-9

Frequently Asked Questions

What causes Run-time error 13 'Type mismatch' in VBA?

This error occurs when your code tries to assign a value to a variable that doesn't match its declared data type, or when a worksheet function evaluation (like using brackets `[]`) fails due to incorrect syntax and returns an error instead of an expected numeric value.

Why shouldn't I use COUNTA to find the last row in a VBA macro?

COUNTA simply counts the number of non-empty cells in a range. If your data contains blank cells in the middle of a column, the total count will be less than the actual last row number. This causes the macro to identify the wrong row and overwrite existing data.

Can I run my Excel VBA macros in WPS Office?

Yes, WPS Office fully supports VBA macros. You can open your `.xlsm` files, execute existing macros, and modify your code using the built-in VBA editor found under the Developer tab.