Find last number in column that includes text and return value

Chestnuttgirl98

New Member
Joined
Apr 18, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I need to find the last occurrence of a number in a column that will also have text in some of the cells. For example, the column is tracking monthly variance. In future months, there is no variance so the cell returns NA(). I need to find the last number above the first occurrence of NA(), because i need to use that value in a different calculation.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If the only errors in the column are #N/A type this UDF may help. Otherwise it will return the last number preceding the first error in the column.
Book1
ABC
133890
2563
310
4855
5204
6105
7688
8912
9667
10617
11127
12325
13699
14805
15894
16589
17310
18105
1990
20#N/A
21634
22768
23884
24#DIV/0!
25315
26165
27944
28#N/A
29580
A
Cell Formulas
RangeFormula
C1C1=LastNumBeforeNA(A:A)
A20,A28A20=NA()
A24A24=1/B24

VBA Code:
Function LastNumBeforeNA(R As Range) As Variant
Dim c As Variant, FirstNA As Variant
For Each c In R
If IsError(c) Then
    Set FirstNA = c
    Exit For
End If
Next c
If Not FirstNA Is Nothing Then
    Set R = Range(R.Cells(1, 1), FirstNA.Offset(-1, 0))
    LastNumBeforeNA = Evaluate("Index(" & R.Address & ",Match(9.99E307," & R.Address & "))")
Else
    LastNumBeforeNA = CVErr(xlErrNA)
    Exit Function
End If
End Function
 
Upvote 0
Here is another way to write the LastNumBeforeNA function...
VBA Code:
Function LastNumBeforeNA(R As Range) As Variant
  LastNumBeforeNA = Cells(Evaluate("MIN(IF(ISNA(" & R.Address & "),ROW(" & R.Address & ")))") - 1, "A").Value
End Function
The above function will skip over any errors other than #N/A errors. The following function will give the last number before ANY error (if that turns out to be a desired functionality)...
VBA Code:
Function LastNumBeforeAnyError(R As Range) As Variant
  LastNumBeforeAnyError = Cells(Evaluate("MIN(IF(ISERROR(" & R.Address & "),ROW(" & R.Address & ")))") - 1, "A").Value
End Function
Note: Like JoeMo's code, both of my code's above will return an error if cell A1 contains an error (because there is no cell before A1 so there is no value to retrieve).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top