Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi all. Could anyone help me make a vba function, that returns the
last numeric (positive or negative) cell in a column. for example in range (F6:F19): cell value F6 5 F7 10 F8 100 F9 169 F10 - F11 - .. - .. - .. - F19 - F20 =LastNumInColumn (F6:F19) OR 169 Please Note : "-" is a hypen Thanks in advance guys. Jeff |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Function LastNumInColumn(r As Range) As Variant
LastNumInColumn = "" For Each rr In r If Application.WorksheetFunction.IsNumber(rr.Value) Then LastNumInColumn = rr.Value Next End Function will work for column, rows, or nearly any range. -- Gary''s Student - gsnu200743 "Jeff" wrote: Hi all. Could anyone help me make a vba function, that returns the last numeric (positive or negative) cell in a column. for example in range (F6:F19): cell value F6 5 F7 10 F8 100 F9 169 F10 - F11 - .. - .. - .. - F19 - F20 =LastNumInColumn (F6:F19) OR 169 Please Note : "-" is a hypen Thanks in advance guys. Jeff |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Something like this should work and is pretty quick:
Function LastCellWithNumber(rngRange As Range, _ Optional bRightToLeft As Boolean) As Range Dim r As Long Dim c As Long Dim arr Dim UB1 As Long Dim UB2 As Long arr = rngRange If rngRange.Cells.Count = 1 Then If IsNumeric(arr) Then Set LastCellWithNumber = rngRange End If Exit Function End If UB1 = UBound(arr) UB2 = UBound(arr, 2) If bRightToLeft Then For c = UB2 To 1 Step -1 For r = UB1 To 1 Step -1 If Not IsEmpty(arr(r, c)) Then If IsNumeric(arr(r, c)) Then Set LastCellWithNumber = _ Cells((r + rngRange.Cells(1).Row) - 1, _ (c + rngRange.Cells(1).Column) - 1) Exit Function End If End If Next r Next c Else For r = UB1 To 1 Step -1 For c = UB2 To 1 Step -1 If Not IsEmpty(arr(r, c)) Then If IsNumeric(arr(r, c)) Then Set LastCellWithNumber = _ Cells((r + rngRange.Cells(1).Row) - 1, _ (c + rngRange.Cells(1).Column) - 1) Exit Function End If End If Next c Next r End If End Function Sub test() Dim rngLastNumber As Range Set rngLastNumber = LastCellWithNumber(Range(Cells(1), Cells(20, 1)), False) If Not rngLastNumber Is Nothing Then MsgBox rngLastNumber.Address End If End Sub Bear in mind that the function could return Nothing, so you will have to handle this if you are using this as a worksheet function. RBS "Jeff" wrote in message ps.com... Hi all. Could anyone help me make a vba function, that returns the last numeric (positive or negative) cell in a column. for example in range (F6:F19): cell value F6 5 F7 10 F8 100 F9 169 F10 - F11 - . - . - . - F19 - F20 =LastNumInColumn (F6:F19) OR 169 Please Note : "-" is a hypen Thanks in advance guys. Jeff |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
There is no need to write code. Bob Phillips has collected some of the
cleverest methods posted in the news groups at his site: http://xldynamic.com/source/xld.LastValue.html -- Regards, Tom Ogilvy "Jeff" wrote: Hi all. Could anyone help me make a vba function, that returns the last numeric (positive or negative) cell in a column. for example in range (F6:F19): cell value F6 5 F7 10 F8 100 F9 169 F10 - F11 - .. - .. - .. - F19 - F20 =LastNumInColumn (F6:F19) OR 169 Please Note : "-" is a hypen Thanks in advance guys. Jeff |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Alpha with numeric and numeric only numbers in a column | Excel Discussion (Misc queries) | |||
find alpha in column 1 numeric column 2 | Excel Discussion (Misc queries) | |||
How do I format the A/ B Column to be fixed like numeric column | Excel Worksheet Functions | |||
How do I format the A/ B Column to be fixed like numeric column | Excel Worksheet Functions | |||
Find Numeric Criterion in Column & Return the Numeric Value from Row above | Excel Worksheet Functions |