Excel VBA: Creating a User Defined Function to Find the Last Non-Blank Cell in a Column
Excel VBA: Creating a User Defined Function to Find the Last Non-Blank Cell in a Column

Have you ever needed to find the last non-blank cell in a column using a User Defined Function (UDF) in Excel? Many users struggle with this common task, especially when transitioning from formulas to VBA. This guide will walk you through creating an effective UDF to identify the last populated cell in a column.
Why This Problem Happens
The challenge arises because many Excel users are accustomed to using array formulas like LOOKUP or INDEX/MATCH to find the last non-blank cell. When they attempt to convert this logic into a UDF, they often encounter errors due to differences in how VBA handles ranges and data.
Step-by-Step Solution
Let’s break down the process of creating a robust UDF for finding the last non-blank cell in a column.
Setting Up Your Environment
- Open Excel and press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- Insert a new module by clicking Insert > Module.
- Copy and paste the following code into the module:
Function LastInColumn(rng As Range) As Variant
Dim lRow As Long
On Error Resume Next
lRow = rng.Rows.Count ' Get the count of rows in the range
Do While rng.Cells(lRow, 1).Value = ""
lRow = lRow - 1
If lRow 0 Then
LastInColumn = rng.Cells(lRow, 1).Value
Else
LastInColumn = "No Data"
End If
End Function
Explanation of the Code
- Dim lRow As Long: This declares a variable to store the last row number.
- On Error Resume Next: This allows the code to continue running even if an error occurs (e.g., trying to access a cell beyond the range).
- Do While Loop: This loop decrements the row counter until it finds a non-blank cell. If it reaches the top of the column without finding any data, it exits.
- On Error GoTo 0: This resets error handling to its default state.
- If lRow > 0 Then: If a non-blank cell is found, it returns the value of that cell. Otherwise, it returns “No Data”.
Using Your UDF in Excel
- Return to your Excel workbook.
- In any cell, enter the formula =LastInColumn(A:A), replacing A:A with the column you want to analyze.
- Press Enter. The cell will display the value of the last non-blank cell in the specified column.

Advanced Variation: Handling Dates and Special Characters
If your column contains dates or special characters, you might want to refine the UDF to handle these cases explicitly.
Function LastInColumnAdvanced(rng As Range) As Variant
Dim lRow As Long
On Error Resume Next
lRow = rng.Rows.Count
Do While IsEmpty(rng.Cells(lRow, 1)) Or IsDate(rng.Cells(lRow, 1).Value)
lRow = lRow - 1
If lRow 0 Then
LastInColumnAdvanced = rng.Cells(lRow, 1).Value2 ' Use Value2 to handle dates correctly
Else
LastInColumnAdvanced = "No Data"
End If
End Function
This advanced version uses IsDate to handle date values and Value2 to correctly interpret cell contents, particularly useful when dealing with date values.
Common Mistakes and Misconceptions
- Using Integer Instead of Long: Always use Long for row counters. Using Integer can cause overflow errors with larger datasets.
- Ignoring Special Characters: Remember to account for special characters and dates, which might not be recognized as empty cells but still need handling.
- Error Handling: Not implementing proper error handling can lead to unexpected crashes or incorrect results.
Conclusion and Tool Recommendation
Creating a UDF in Excel VBA to find the last non-blank cell in a column can significantly enhance your data analysis capabilities. This guide provided a step-by-step solution, an advanced variation for handling special cases, and highlighted common pitfalls to avoid.
For more advanced Excel techniques and automation tools, consider exploring CelTools, which offers 70+ additional features for auditing, formulas, and automation. This powerful add-in can further streamline your workflow and unlock new possibilities within Excel.
Written By: Ada Codewell – AI Specialist & Software Engineer at Gray Technical






















