Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Okay, people. Can someone see an error in this UDF code??? I'm coming
up with a "#VALUE!" error. Public Function findfuel(rng As Range) Dim rCell As Range Dim mytotal As Double Dim numberofcells As Integer Dim myaverage As Double Const sStr As String = "fuel" For Each rCell In rng If InStr(1, rCell.Comment.Text, sStr, vbTextCompare) Then mytotal = mytotal + rCell.Value numberofcells = numberofcells + 1 myaverage = mytotal / numberofcells End If Next rCell findfuel = average End Function Seems pretty straightforward. Any help would be appreciated. Thanks. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The last line of your UDF sets findfule = average not myaverage but I
think the main problem is that this will error out if any cell in you range does not have a comment. Try it like this: Public Function findfuel(rng As Range) Dim rCell As Range Dim mytotal As Double Dim numberofcells As Integer Dim myaverage As Double Const sStr As String = "fuel" Dim theCmt As Comment For Each rCell In rng Set theCmt = rCell.Comment If Not theCmt Is Nothing Then If InStr(1, theCmt.Text, sStr, vbTextCompare) 0 Then mytotal = mytotal + rCell.Value numberofcells = numberofcells + 1 End If End If Next rCell findfuel = mytotal / numberofcells End Function Note this will not recalculate when comments are changed. Hope this helps Rowan bruch04 wrote: Okay, people. Can someone see an error in this UDF code??? I'm coming up with a "#VALUE!" error. Public Function findfuel(rng As Range) Dim rCell As Range Dim mytotal As Double Dim numberofcells As Integer Dim myaverage As Double Const sStr As String = "fuel" For Each rCell In rng If InStr(1, rCell.Comment.Text, sStr, vbTextCompare) Then mytotal = mytotal + rCell.Value numberofcells = numberofcells + 1 myaverage = mytotal / numberofcells End If Next rCell findfuel = average End Function Seems pretty straightforward. Any help would be appreciated. Thanks. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
That worked perfectly. Thanks so much.
|
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You're welcome.
bruch04 wrote: That worked perfectly. Thanks so much. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Code to copy the formulae of one cell to all the cell in the rangewith the specific cell and columnnumber changing Options | Excel Discussion (Misc queries) | |||
Extract text within two specific values within a cell | Excel Worksheet Functions | |||
select date range then find average of values in another cell | Excel Worksheet Functions | |||
find specific text and make cell highlighted | Excel Discussion (Misc queries) | |||
Can Comments be automatically converted to text cell values? | Excel Discussion (Misc queries) |