Hi all,
This is my first time posting, but I read a few stickies so hopefully my syntax isn't terrible. I can't post the actual sheet I'm working on, as it contains sensitive information. However, I've described my problem and posted my code. If needed, I can probably make a dupe spreadsheet to post here.
I'm having trouble with a custom function that calculates a coefficient of variation for a given range, and compares it to a max acceptable value, then colors the cell accordingly.
Basically, as soon as the function executes any line of code using ranges, the all the values in Watch go "<out of context>", and the function aborts (i've set a breakpoint on End Function that never gets hit). The only times I can make references to ranges is when using MsgBox (the two lines below I made for debugging). All of this is kind of perplexing me.
I'm afraid I'm just making a stupid mistake somewhere. I do know how to debug, and have been stepping through the code and watching variables.
Here's the function code:
[pre]<br />
<pre><code>Function RequirementD96(data As Range, Optional MaxCOV As Double = 5) As Double
'actual calculation part of function; works fine:
RequirementD96 = ((Application.WorksheetFunction.StDev(data)) / (Application.WorksheetFunction.Average(data)))
'code I made to debug
Range("B1:B6".Interior.Color = RGB(200, 160, 35)
MsgBox Application.Caller.Address
MsgBox Application.Range("G3".Address
'' this is code that I want to do, but was ran into the range problems, so I put it on hold.
'Application.Caller.DisplayFormat.NumberFormat = "Percentage"
'If (RequirementD96 <= (MaxCOV / 100)) Then 'the data passes the requirement
'Application.ActiveCell.Interior.Color = vbGreen
'Else
'Application.ActiveCell.Interior.Color = vbRed
'End If
End Function</code></pre>
<p> [/pre]
Thanks for any insight you might be able to provide. I think I've combed through 15 other forums looking for help, but the closest thing to an answer was on here.
This is my first time posting, but I read a few stickies so hopefully my syntax isn't terrible. I can't post the actual sheet I'm working on, as it contains sensitive information. However, I've described my problem and posted my code. If needed, I can probably make a dupe spreadsheet to post here.
I'm having trouble with a custom function that calculates a coefficient of variation for a given range, and compares it to a max acceptable value, then colors the cell accordingly.
Basically, as soon as the function executes any line of code using ranges, the all the values in Watch go "<out of context>", and the function aborts (i've set a breakpoint on End Function that never gets hit). The only times I can make references to ranges is when using MsgBox (the two lines below I made for debugging). All of this is kind of perplexing me.
I'm afraid I'm just making a stupid mistake somewhere. I do know how to debug, and have been stepping through the code and watching variables.
Here's the function code:
[pre]<br />
<pre><code>Function RequirementD96(data As Range, Optional MaxCOV As Double = 5) As Double
'actual calculation part of function; works fine:
RequirementD96 = ((Application.WorksheetFunction.StDev(data)) / (Application.WorksheetFunction.Average(data)))
'code I made to debug
Range("B1:B6".Interior.Color = RGB(200, 160, 35)
MsgBox Application.Caller.Address
MsgBox Application.Range("G3".Address
'' this is code that I want to do, but was ran into the range problems, so I put it on hold.
'Application.Caller.DisplayFormat.NumberFormat = "Percentage"
'If (RequirementD96 <= (MaxCOV / 100)) Then 'the data passes the requirement
'Application.ActiveCell.Interior.Color = vbGreen
'Else
'Application.ActiveCell.Interior.Color = vbRed
'End If
End Function</code></pre>
<p> [/pre]
Thanks for any insight you might be able to provide. I think I've combed through 15 other forums looking for help, but the closest thing to an answer was on here.