LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Random numbers appearing, calculations showing as zero


Code:
--------------------
Function CustomAverage(pRange As Range, pThreshold As Long) As Long

Dim LFirstRow, LLastRow As Integer
Dim LFirstCol, LLastCol As Integer

Dim LCurrentRow As Integer
Dim LCurrentCol As Integer

Dim LTotal As Double
Dim LCount As Integer

On Error GoTo Err_Execute

'Determine first and last row to average
LFirstRow = pRange.Row
LLastRow = LFirstRow + pRange.Rows.Count - 1

'Determine first and last column to average
LFirstCol = pRange.Column
LLastCol = LFirstCol + pRange.Columns.Count - 1

'Initialize variables
LTotal = 0
LCount = 0

'Move through each cell in the range and include in the average
' calculation if the value = pThreshold
For LCurrentCol = LFirstCol To LLastCol
For LCurrentRow = LFirstRow To LLastRow
If Cells(LCurrentRow, LCurrentCol) = pThreshold Then
LTotal = LTotal + Cells(LCurrentRow, LCurrentCol)
LCount = LCount + 1
End If
Next
Next

'Return the average

' check that LCount is not zero and return 0 average if so, this happens in the case
' that all values were below the threshold or null.. which would cause trouble (error)
If LCount = 0 Then
CustomAverage = 0
Else
CustomAverage = LTotal / LCount
End If

On Error GoTo 0

Exit Function

Err_Execute:
CustomAverage = 0
'MsgBox "An error occurred while calculating the Custom Average."

End Function

--------------------


Hi, I was wondering if I could have some help, okay, well the code
above works perfectly, does exactly what I want it to, but it's a bit
buggy, well the spreadsheet is anyway, basically, random numbers will
appear in cells which don't have numbers in them(i.e the custom average
of 4 blank cells will return a "60" as a value, and zeros will appear
where there should be calculations using the custom average function,
i,e the custom average of 60,40, 10 will be zero)
If you click into the cell and press enter, or if you auto fill the
formula (drag it over) from neighbour cells then the calculation will
refresh and will be correct/the zeros will appear/dissapear. obviously
this is a big spreadsheet, and I can't click in every single cell by
hand, is there a way to auto refresh the entire spreadsheet, or
possibly, is there a way to stop it from happening?
If anyone has any ideas then it would be much appreciated
Thanks!


--
ophelia
------------------------------------------------------------------------
ophelia's Profile: http://www.excelforum.com/member.php...o&userid=33778
View this thread: http://www.excelforum.com/showthread...hreadid=535507

 
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
Pivot table NOT showing zero value calculations Jaimed1010 Excel Worksheet Functions 2 November 7th 07 12:24 AM
Non-random numbers generated by excel's data analysis random gener Allie Excel Worksheet Functions 10 September 17th 05 06:19 AM
Non-random numbers generated by excel's data analysis random gener Harlan Grove Excel Discussion (Misc queries) 2 September 13th 05 04:06 PM
Showing a unique random number w/o duplicates tx12345 Excel Worksheet Functions 4 August 27th 05 02:51 AM
How do I set up a standard form showing incentive calculations KeithB Excel Discussion (Misc queries) 2 May 30th 05 09:41 AM


All times are GMT +1. The time now is 07:12 AM.

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"