PC Review


Reply
Thread Tools Rate Thread

Custom function acting weird

 
 
Riddler
Guest
Posts: n/a
 
      11th Jan 2007
I have a custom function (see below) that is used on several cells on
sheet 1. It all works fine except for when I make a copy of the sheet
into the same workbook and it calculates with some new numbers. What
happens is that sheet 1 and sheet 2 now show the same results. When I
recalc on the sheet that is wrong it messes up the other one. It seems
that when I watch the myRange.address as it recalculates all the
occurences of this function on sheet 1 & 2 it uses the same cell range
(from the active sheet) for all and this is why the other sheets do not
get their data calculated correctly.
So how do I make this custom function use the range from the sheet that
contains each of the cells that use this function?

Thanks for any help or direction you can give me.

Scott

Function LowestRepeatableNumber(myRange As Range)
Application.Volatile
'This function returns the second lowest number above zero if there are
no repeating numbers
LowestRepeatableNumber = Evaluate("MIN(IF(" & myRange.Address &
">0," & "IF(COUNTIF(" & myRange.Address & "," & myRange.Address &
")>1," & _
myRange.Address & ")))")
If LowestRepeatableNumber = 0 Then LowestRepeatableNumber =
Evaluate("small(IF(" & myRange.Address & ">0," & "IF(COUNTIF(" &
myRange.Address & "," & myRange.Address & ")=1," & _

myRange.Address & ")),2)")


End Function

 
Reply With Quote
 
 
 
 
Charles Williams
Guest
Posts: n/a
 
      11th Jan 2007
The Application.Evaluate method (which is the default for Evaluate) always
assumes that any unqualified range reference refers to the active sheet.
Therefore its much safer to use the Worksheet.evaluate method:

Application.Caller.Parent.Evaluate( ....)

this will use the parent of the cell containing the custom function (ie the
worksheet containing the custom function) as the worksheet for any
unqualified range reference.

(note there are some other "quirks" of Evaluate that are worth knowing if
you make much use of it: see
http://www.decisionmodels.com/calcsecretsh.htm for details)

hth
Charles
______________________
Decision Models
FastExcel 2.3 now available
Name Manager 4.0 now available
www.DecisionModels.com

"Riddler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a custom function (see below) that is used on several cells on
> sheet 1. It all works fine except for when I make a copy of the sheet
> into the same workbook and it calculates with some new numbers. What
> happens is that sheet 1 and sheet 2 now show the same results. When I
> recalc on the sheet that is wrong it messes up the other one. It seems
> that when I watch the myRange.address as it recalculates all the
> occurences of this function on sheet 1 & 2 it uses the same cell range
> (from the active sheet) for all and this is why the other sheets do not
> get their data calculated correctly.
> So how do I make this custom function use the range from the sheet that
> contains each of the cells that use this function?
>
> Thanks for any help or direction you can give me.
>
> Scott
>
> Function LowestRepeatableNumber(myRange As Range)
> Application.Volatile
> 'This function returns the second lowest number above zero if there are
> no repeating numbers
> LowestRepeatableNumber = Evaluate("MIN(IF(" & myRange.Address &
> ">0," & "IF(COUNTIF(" & myRange.Address & "," & myRange.Address &
> ")>1," & _
> myRange.Address & ")))")
> If LowestRepeatableNumber = 0 Then LowestRepeatableNumber =
> Evaluate("small(IF(" & myRange.Address & ">0," & "IF(COUNTIF(" &
> myRange.Address & "," & myRange.Address & ")=1," & _
>
> myRange.Address & ")),2)")
>
>
> End Function
>



 
Reply With Quote
 
Riddler
Guest
Posts: n/a
 
      11th Jan 2007
Application.Caller.Parent.Evaluate( ....) Worked great!

Thanks a bunch

Scott



Charles Williams wrote:
> The Application.Evaluate method (which is the default for Evaluate) always
> assumes that any unqualified range reference refers to the active sheet.
> Therefore its much safer to use the Worksheet.evaluate method:
>
> Application.Caller.Parent.Evaluate( ....)
>
> this will use the parent of the cell containing the custom function (ie the
> worksheet containing the custom function) as the worksheet for any
> unqualified range reference.
>
> (note there are some other "quirks" of Evaluate that are worth knowing if
> you make much use of it: see
> http://www.decisionmodels.com/calcsecretsh.htm for details)
>
> hth
> Charles
> ______________________
> Decision Models
> FastExcel 2.3 now available
> Name Manager 4.0 now available
> www.DecisionModels.com
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function keys acting weird DigitalBlade Windows Vista General Discussion 1 16th Sep 2007 09:12 PM
VBE acting weird - help dpenny Microsoft Excel Programming 1 28th Jan 2006 05:25 AM
xp acting weird?? DP Windows XP New Users 0 23rd Oct 2005 12:48 PM
IE acting weird HELP!! Windows XP Internet Explorer 0 31st May 2004 07:07 AM
XP Pro Acting Weird myrt webb Windows XP Setup 1 3rd Apr 2004 01:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:21 PM.