Get last word from column that is not "NA"

eaje92

New Member
Joined
Feb 19, 2018
Messages
16
Hi all, I need some help in using vba to get the last Letter/Word from a column while the values below it are set as "NA"

E.g

0A
1B
NA
NA
NA
NA
NA

<tbody>
</tbody>

In this case, the value of 1B will be copied/stored.

Thank you in advance!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This code finds the last value that is not NA and displays it in a messagebox. It also assumes that your data is in column A.

Code:
Sub GetLastNotNA()
Dim LastVal As String
Dim AR()


AR = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row()).Value


For i = UBound(AR) To 1 Step -1
    If AR(i, 1) <> "NA" Then
        LastVal = AR(i, 1)
        Exit For
    End If
Next i


MsgBox LastVal
End Sub
 
Upvote 0
And before Rick does it, here's a non looping version.

Code:
Sub GetLastNoLoop()
Dim LastVal As String
Dim LR As Long


LR = Range("A" & Rows.Count).End(xlUp).Row()
LastVal = Evaluate(Replace("Lookup(2,1/(A1:A#<>""NA""),A1:A#)", "#", LR))


MsgBox LastVal
End Sub
 
Last edited:
Upvote 0
And before Rick does it...
:ROFLMAO:



...here's a non looping version.
Code:
Sub GetLastNoLoop()
  Dim LastVal As String
  Dim LR As Long
  LR = Range("A" & Rows.Count).End(xlUp).Row()
  LastVal = Evaluate(Replace("Lookup(2,1/(A1:A#<>""NA""),A1:A#)", "#", LR))
  MsgBox LastVal
End Sub
I probably would have written it this way instead though...
Code:
Sub GetLastNotNA()
  Dim LastVal As Variant
  LastVal = [A:A].Find("NA", , xlValues, xlWhole, , xlNext, , , False).Offset(-1)
  MsgBox LastVal
End Sub
 
Last edited:
Upvote 0
I love seeing these no-loop versions. They are super interesting. Thanks for laying down the knowledge Rick!
 
Upvote 0
I love seeing these no-loop versions. They are super interesting.
I agree... as it turns out, there is a lot of functionality hiding inside the Excel object model extensions to the core Visual Basic language.



Thanks for laying down the knowledge Rick!
To the small extent I am able to do so, you are quite welcome.
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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