VBA to get last value in range and return column...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, this has been driving me crazy...

I have a range (EF60:IV60) and I need to return the last value which is not blank or 0. I'm also looking to return the column that this value is sitting in...

(I've tried this with conventional formulas, but it is not reliable for some reason)

If you can point me in the right direction, I'd be most grateful

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Would there possibily be anything in row 60 AFTER column IV, or not?
 
Upvote 0
Try this:
Code:
Sub MyMacro()

    Dim lastCell As Range

    If Range("IV60") <> "" Then
        Set lastCell = Range("IV60")
    Else
        Set lastCell = Range("IX60").End(xlToLeft)
    End If
        
    If lastCell.Column >= 136 Then
        MsgBox "Last value is in cell " & lastCell.Address(0, 0) & " and is " & lastCell
    Else
        MsgBox "There are no values in the range EF60:IV60"
    End If

End Sub
 
Upvote 0
Brilliant, thanks for that... I'll give it a whirl

Cheers
 
Upvote 0
Here is another macro that you can try...
Code:
[table="width: 500"]
[tr]
	[td]Sub LastNonZeroNoBlankCell()
  Dim lastCell As Range
  Set lastCell = Cells(60, 135 + Len(RTrim(Join(Evaluate("IF(EF60:IV60<>0,""X"","" "")"), ""))))
  MsgBox "Last cell's value: " & lastCell.Value
  MsgBox "Last cell's address: " & lastCell.Address
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Here is another macro that you can try...
It produces some odd results if no cells are populated in that range.
 
Upvote 0
It produces some odd results if no cells are populated in that range.
I forgot to include a check for that. Here is my code revised to handle this situation should it arise (plus I made a couple of minor changes, one because the range is fixed and non-changing)...
Code:
[table="width: 500"]
[tr]
	[td]Sub LastNonZeroNoBlankCell()
  Dim LastCell As Range
  Set LastCell = Cells(60, 135 + Len(RTrim(Join([IF(EF60:IV60<>0,"X"," ")], ""))))
  If LastCell.Address(0, 0) = "EE60" Then
    MsgBox "There are no non-zero values in the range EF60:IV60"
  Else
    MsgBox "Last cell's value: " & LastCell.Value
    MsgBox "Last cell's address: " & LastCell.Address(0, 0)
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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