How to get the value of the last non-empty cell in a row?
-
14-11-2019 - |
Question
Here is a sample spreadsheet
I want to select a range (ex: A2:E2
) and get the value 1
. If I select A4:E4
I will get the value 1
; and so on. Is there any way to achieve this with a macro?
Another thing I would need is the value in the first row of the corresponding column. Ex:
If I select A2:E2
I want to get n
(since 1 is in column E), if I select A4:E4
I want to get v
(since 1 is in column C).
Solution
Ok, here is a function which you can use as a formula in the workbook. It also addresses both your questions. :)
Paste the below code in a module.
SYNTAX/USAGE
=MyFunc(Cell Range, Arg)
Cell Range: (Required) A range such as A2:E2
Arg: (Required) Can take a value of "H" Or "R". "H" will give you the column Header and "R" will give you the row value
Example
=MyFunc(A1:E2,"H") will give you "n" AND
=MyFunc(A1:E2,"R") will give you "1"
Option Explicit
Function MyFunc(rng As Range, stype As String) As Variant
Dim MyArray() As String
Dim sCol As Long
If stype = "" Or (UCase(stype) <> "H" And _
UCase(stype) <> "R") Then Exit Function
With rng
MyArray = Split(.Address, "$")
sCol = Cells(Val(MyArray(UBound(MyArray))), _
Columns.Count).End(xlToLeft).Column
If UCase(stype) = "H" Then
MyFunc = Cells(1, sCol)
ElseIf UCase(stype) = "R" Then
MyFunc = Range(MyArray(UBound(MyArray) - 1) & _
MyArray(UBound(MyArray))).Value
End If
End With
End Function
Note: This is just a general code to show you how it works and it doesn't handle any errors at the moment.
OTHER TIPS
The following code should do what you want. Note: I am not selecting anything since this slows things down to no advantage.
Dim ColLast as Long
Dim ValueColLast as String
Dim HeadColLast as String
With Sheets(xxxxx)
ColLast = .Cells(2,Columns.Count).End(xlToLeft).Column
ValueColLast = .Cells(2, ColLast).Value
HeadColLast = .Cells(1,ColLast).Value
End With
Cells(Row, Column)
addresses the cell identified by Row and Column in the active worksheet. Row must be a number. Column can be a number (eg 1 or 104) or a column identifier (eg "A" or "CZ")
.Cells(Row, Column)
addresses the cell identified by Row and Column in the worksheet identified in the With statement. Note the leading dot.
.Cells(2,Columns.Count)
addresses the last cell in row 2 since Columns.Count
given the number of columns per worksheet for the current version of Excel. `Rows.Count
does the same for rows.
Assuming .Cells(2,Columns.Count)
is blank, .Cells(2,Columns.Count).End(xlToLeft)
finds the next cell in the specified direction with a value. This is the VBA equivalent of Ctrl+LeftArrow. xlToRight
, xlUp
and xlDown
allow you to move in the other directions. .Cells(Rows.Count,"A").End(xlUp)
gives the last used row in Column A.
.Cells(2,Columns.Count).End(xlToLeft).Column
gives the number of the last used column in row 2.
ValueColLast = .Cells(2, ColLast).Value
sets ValueColLast to the value of the last cell in Row 2. I have defined ValueColLast as a String because, if I assumed it was a number and defined it as Long, I would get an error if it was not.
HeadColLast = .Cells(1,ColLast).Value
sets HeadColLast to value in Row 1.
Hope this all makes sense.