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

[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]0A[/TD]
[/TR]
[TR]
[TD]1B[/TD]
[/TR]
[TR]
[TD]NA[/TD]
[/TR]
[TR]
[TD]NA[/TD]
[/TR]
[TR]
[TD]NA[/TD]
[/TR]
[TR]
[TD]NA[/TD]
[/TR]
[TR]
[TD]NA[/TD]
[/TR]
</tbody>[/TABLE]

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

Thank you in advance!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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...
:rofl:



...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,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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