Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Last Numeric in a Column?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Last Numeric in a Column?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Last Numeric in a Column?

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Last Numeric in a Column?

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Alpha with numeric and numeric only numbers in a column rciolkosz Excel Discussion (Misc queries) 6 September 18th 09 09:31 PM
find alpha in column 1 numeric column 2 JANeyer Excel Discussion (Misc queries) 2 February 26th 08 04:44 AM
How do I format the A/ B Column to be fixed like numeric column RYarn Excel Worksheet Functions 1 August 17th 07 02:18 AM
How do I format the A/ B Column to be fixed like numeric column bj Excel Worksheet Functions 0 August 17th 07 02:04 AM
Find Numeric Criterion in Column & Return the Numeric Value from Row above Sam via OfficeKB.com Excel Worksheet Functions 6 April 27th 06 02:50 PM


All times are GMT +1. The time now is 12:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"