Modifying custom function to find second last instead of last

BarinaX

New Member
Joined
Dec 25, 2014
Messages
21
Hi all,


I've been using this function to find the last value in column. Now I would need it modified to find second to last instead. How do I change it?

Code:
Function LASTINCOLUMN(rng As Range)
    Dim LastCell As Range
    Application.Volatile
    With rng.Parent
        With .Cells(.Rows.Count, rng.Column)
            If Not IsEmpty(.Value) Then
                LASTINCOLUMN = .Value
            ElseIf IsEmpty(.End(xlUp)) Then
                LASTINCOLUMN = " "
            Else
                LASTINCOLUMN = .End(xlUp).Value
            End If
        End With
    End With
End Function
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,

Instead of saving .value, I suggest you save .row. That way, you can set LASTINCOLUMN to cells(LASTINCOLUMNrow-1,rng.Column).Value to get the value before the last value in the column.

Does this work for you?

Paul
 
Upvote 0
See if this function works for you...
Code:
Function SecondToLastValue(Rng As Range)
  Dim X As Long, LastRow As Long
  Application.Volatile
  If Rng.Columns.Count = 1 Then
    LastRow = Cells(Rows.Count, Rng.Column).End(xlUp).Row
    For X = LastRow - 1 To 1 Step -1
      If Len(Cells(X, Rng.Column).Value) Then
        SecondToLastValue = Cells(X, Rng.Column).Value
        Exit Function
      End If
    Next
  End If
End Function
 
Upvote 0
Sorry, that only gives me zero as a result. I presume that this is because the data column has empty cells in between number cells? So that the function you posted only makes the search based on first empty, not the last filled cell.
 
Upvote 0
Sorry, that only gives me zero as a result.
Okay, see if this modification to my function works for you or not...

Code:
Function SecondToLastValue(Rng As Range)
  Dim X As Long, LastRow As Long
  Application.Volatile
  If Rng.Columns.Count = 1 Then
    LastRow = Rng.EntireColumn.Find("*", , xlValues, , xlRows, xlPrevious).Row
    For X = LastRow - 1 To 1 Step -1
      If Len(Cells(X, Rng.Column).Value) Then
        SecondToLastValue = Cells(X, Rng.Column).Value
        Exit Function
      End If
    Next
  End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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