Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 183
Default VBA Function returns #VALUE! when working on another book

I create a function called BassDiff1()
I put it in General/Standard module:VBAProject\Modules\Module1
I an trying to use it in a worsheet formula: example: =BassDiff1(L37)
It works fine except when I start working on another workbook and come back,
it displays #VALUE!
this is the function:
Any help greatly appreciated
thanks

Option Explicit
Public NDataPoints As Integer
Dim Vara, Varb, Varc As Long
Public InputDataCell1, SummaryOutput, YVar As Range

Function BassDiff1(num)

'VARIABLES
Dim m1 As Variant
Dim p1 As Variant
Dim q1 As Variant

NDataPoints = Range("NDataPoints").Value
Set YVar = Range(Range("YVarCell1"), Range("YVarCell1").Offset(NDataPoints -
1, 0))
Vara = Range("Vara").Value
Varb = Range("Varb").Value
Varc = Range("Varc").Value

m1 = Application.WorksheetFunction.Sum(YVar)
p1 = Varc / m1
q1 = p1 + Vara

'FUNCTION
BassDiff1 = p1 * (m1 - num) + (q1 * (num / m1) * (m1 - num))

End Function


--
caroline
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default VBA Function returns #VALUE! when working on another book

Here are some suggestions:

Dim Vara, Varb, Varc As Long defines Vara and Varb as variant not long,
which is probably not what you meant

It's not a good idea to reference Defined Names or ranges inside a UDF when
they have not been passed in as parameters: you may get unexpected results
because Excel's calculation engine does not expect this. I suggest you make
all the ranges the UDF references into parameters for the UDF.

I don't know why you are declaring some of the variables as Public, some as
Module scope and others inside the function, but I suggest you move all your
declarations to inside the function.

Using Application.WorksheetFunction.SUM inside a UDF will sometimes return
#Value the first time its recalculated: try pressing Ctrl/Alt/F9.


Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com

"caroline" wrote in message
...
I create a function called BassDiff1()
I put it in General/Standard module:VBAProject\Modules\Module1
I an trying to use it in a worsheet formula: example: =BassDiff1(L37)
It works fine except when I start working on another workbook and come
back,
it displays #VALUE!
this is the function:
Any help greatly appreciated
thanks

Option Explicit
Public NDataPoints As Integer
Dim Vara, Varb, Varc As Long
Public InputDataCell1, SummaryOutput, YVar As Range

Function BassDiff1(num)

'VARIABLES
Dim m1 As Variant
Dim p1 As Variant
Dim q1 As Variant

NDataPoints = Range("NDataPoints").Value
Set YVar = Range(Range("YVarCell1"),
Range("YVarCell1").Offset(NDataPoints -
1, 0))
Vara = Range("Vara").Value
Varb = Range("Varb").Value
Varc = Range("Varc").Value

m1 = Application.WorksheetFunction.Sum(YVar)
p1 = Varc / m1
q1 = p1 + Vara

'FUNCTION
BassDiff1 = p1 * (m1 - num) + (q1 * (num / m1) * (m1 - num))

End Function


--
caroline



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default VBA Function returns #VALUE! when working on another book

yuor function explicitly looks up named ranges. Do these exist on the active
sheet of the other workbooks?


this line is worng:
Dim Vara, Varb, Varc As Long

should be
Dim Vara As Long
Dim Varb As Long
Dim Varc As Long

or
Dim Vara As Long, Varb As Long, Varc As Long

Also, your function doesn't use Varb...is that an oversight?


What is the purpose of this function?









"caroline" wrote:

I create a function called BassDiff1()
I put it in General/Standard module:VBAProject\Modules\Module1
I an trying to use it in a worsheet formula: example: =BassDiff1(L37)
It works fine except when I start working on another workbook and come back,
it displays #VALUE!
this is the function:
Any help greatly appreciated
thanks

Option Explicit
Public NDataPoints As Integer
Dim Vara, Varb, Varc As Long
Public InputDataCell1, SummaryOutput, YVar As Range

Function BassDiff1(num)

'VARIABLES
Dim m1 As Variant
Dim p1 As Variant
Dim q1 As Variant

NDataPoints = Range("NDataPoints").Value
Set YVar = Range(Range("YVarCell1"), Range("YVarCell1").Offset(NDataPoints -
1, 0))
Vara = Range("Vara").Value
Varb = Range("Varb").Value
Varc = Range("Varc").Value

m1 = Application.WorksheetFunction.Sum(YVar)
p1 = Varc / m1
q1 = p1 + Vara

'FUNCTION
BassDiff1 = p1 * (m1 - num) + (q1 * (num / m1) * (m1 - num))

End Function


--
caroline

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
IF function returns function statement rather than result ckrogers Excel Worksheet Functions 3 April 16th 09 09:21 PM
Work book with working days only Thyag Excel Programming 10 August 21st 07 08:58 PM
Newly created Get Function is not working when I copied the syntax from a working function CJ Excel Programming 1 January 16th 07 05:28 AM
Paste into new Excel book not working [email protected] Excel Programming 9 November 17th 06 09:56 AM
Array not working correctly Returns FALSE on second part aaronm49 Excel Discussion (Misc queries) 1 February 11th 05 01:07 AM


All times are GMT +1. The time now is 03:52 AM.

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

About Us

"It's about Microsoft Excel"