VBA & Macro Problems

Find the Last Nonzero Row in Excel Column Q with VBA

Kushani NimanthikaKushani Nimanthika Dec 2, 2026 868 views

Question details

Find the last row in column Q that contains a nonzero value using VBA or an evaluated Excel formula.

Find the Last Nonzero Row in Excel Column Q with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user needs the row number of the last nonzero value within a specified range in column Q.
Observed behavior
The desired result is the greatest row number containing a nonzero value, or an indication that no matching value exists.
Before you start

Check the range you want to search in column Q. The example uses Q2:Q1000 and assumes the cells contain numbers or are blank, with no error values or formulas returning empty text.

Solution 1Recommended

Use VBA Evaluate to Find the Last Nonzero Row in Column Q

Calculate the last matching row directly from an Excel formula inside a VBA macro.

1
Open the VBA editor

In Excel, select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon customization settings.

2
Insert the macro

Select Insert > Module and paste: Sub FindLastNonZero() Dim LastRowNonZero As Long LastRowNonZero = ActiveSheet.Evaluate("MAX((Q2:Q1000<>0)*ROW(Q2:Q1000))") MsgBox "Last nonzero row: " & LastRowNonZero End Sub

3
Run the macro

Activate the worksheet you want to search, then select Developer > Macros, choose FindLastNonZero, and click Run. The message box displays the row number.

4
Adjust the search range

In the module, replace both instances of Q2:Q1000 with the same range that covers your data, then run the macro again.

Use VBA Evaluate to Find the Last Nonzero Row in Column Q
No matching value: The macro returns 0 when the range contains only zeros or blank cells. Error values can cause the calculation to fail.
Free Microsoft Office alternative

Work with Column Q Data in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel workbooks to review and analyze column Q data as you move between office suites; VBA support depends on the WPS edition and configuration.

Review and filter column Q data in a familiar spreadsheet interface.Open and save compatible Microsoft Excel formats, including XLSX.Use a lightweight office suite with a free edition.Move existing spreadsheet files into WPS Office without starting from scratch.
microsoft office alternative - wps office

Frequently Asked Questions

What does the VBA expression return if every value is zero?

For a range containing only numeric zeros or blank cells, the expression returns 0, indicating that no nonzero row was found.

How do I search more rows in column Q?

In the VBA module, change both instances of Q2:Q1000 to the same larger range, such as Q2:Q5000, and run the macro again.

Which worksheet does the macro search?

The example uses ActiveSheet.Evaluate, so activate the worksheet containing your data before running the macro from Developer > Macros.

Can text or errors in column Q affect the result?

Yes. The example assumes numeric values or blank cells. Text, formulas returning empty text, and error values can produce unexpected results or cause the calculation to fail.