สูตรจาก http://www.techonthenet.com/excel/qu...last_value.php
Data range is formatted as text
If your range contains only text values, you could use the following formula:
=INDEX(range,MATCH(REPT("z",255),range))
- where range is the range that you wish to check
For example, if you wanted to find the last text value in column A, you would use the following formula:=INDEX(A:A,MATCH(REPT("z",255),A:A))
For example, if you wanted to find the last text value in range C2:C10, you would use the following formula:=INDEX(C2:C10,MATCH(REPT("z",255),C2:C10))
Data range is formatted as numeric
If your range contains only numbers, you could use the following formula:
=INDEX(range,MATCH(9.99999999999999E+307,range))
- where range is the range that you wish to check
For example, if you wanted to find the last numeric value in column A, you would use the following formula:=INDEX(A:A,MATCH(9.99999999999999E+307,A:A))
For example, if you wanted to find the last numeric value in range C2:C10, you would use the following formula:=INDEX(C2:C10,MATCH(9.99999999999999E+307,C2:C10))
Data range contains both text and numeric
If your range contains both text and numeric values (ie: there must be at least one text and one numeric value in your range) and you want to find the last value, you could use the following formula:
=INDEX(range,MAX(MATCH(9.99999999999999E+307,range),MATCH(REPT("z",255),range)))
- where range if the range that you wish to check
For example, if you wanted to find the last value (either text or numeric) in column A, you would use the following formula:=INDEX(A:A,MAX(MATCH(9.99999999999999E+307,A:A),MATCH(REPT("z",255),A:A)))
For example, if you wanted to find the last value (either text or numeric) in range C2:C10, you would use the following formula:=INDEX(C2:C10,MAX(MATCH(9.99999999999999E+307,C2:C10),MATCH(REPT("z",255),C2:C10)))
======================================
VBA จาก http://j-walk.com/ss/excel/tips/tip30.htm
The LASTINCOLUMN function
Code:
Function LASTINCOLUMN(rngInput As Range)
Dim WorkRange As Range
Dim i As Integer, CellCount As Integer
Application.Volatile
Set WorkRange = rngInput.Columns(1).EntireColumn
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For i = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(i)) Then
LASTINCOLUMN = WorkRange(i).Value
Exit Function
End If
Next i
End Function
The LASTINROW function
Code:
Function LASTINROW(rngInput As Range) As Variant
Dim WorkRange As Range
Dim i As Integer, CellCount As Integer
Application.Volatile
Set WorkRange = rngInput.Rows(1).EntireRow
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For i = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(i)) Then
LASTINROW = WorkRange(i).Value
Exit Function
End If
Next i
End Function
Link อื่นที่น่าสนใจ
http://www.ozgrid.com/VBA/ExcelRanges.htm
http://www.mvps.org/dmcritchie/excel/lastcell.htm