Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 234
Default Function doesn't calculate

I've defined a function in VBA to randomly generate a
number "close to" its argument.
It uses a formula on a hiden sheet, which gives a random
number between 0.666 and 1.333 to multiply the argument by.

Function Near(Actual)
Sheets("hiden").Cells.Calculate
Near = Actual * Sheets("hiden").Cells(1, 1)
End Function

Sub test()
Sheets("hiden").Cells(1, 1) _
.FormulaR1C1 = "=(rand()-0.5)/3+1"
MsgBox Near(100)
End Sub

The test procedure above works, but if I enter the
formula "=near(100)" into a cell on my worksheet and fill
the formula down a few rows, it doesn't calculate the
values. Does anyone know why this is?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Function doesn't calculate

Rob, I'm not an expert (far from it), but try declaring all variables

eg Private Function Near (Actual as Long) As Long

Cheeri

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Function doesn't calculate

add this line to your function:

Application.Volatile

Patrick Molloy
Microsoft Excel MVP
-----Original Message-----
I've defined a function in VBA to randomly generate a
number "close to" its argument.
It uses a formula on a hiden sheet, which gives a random
number between 0.666 and 1.333 to multiply the argument

by.

Function Near(Actual)
Sheets("hiden").Cells.Calculate
Near = Actual * Sheets("hiden").Cells(1, 1)
End Function

Sub test()
Sheets("hiden").Cells(1, 1) _
.FormulaR1C1 = "=(rand()-0.5)/3+1"
MsgBox Near(100)
End Sub

The test procedure above works, but if I enter the
formula "=near(100)" into a cell on my worksheet and

fill
the formula down a few rows, it doesn't calculate the
values. Does anyone know why this is?

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Function doesn't calculate

Hi Rob,

Excel will not let you use the calculate method inside a worksheet function
because it would change something in the excel environment.

You could try using the evaluate method (evaluates a string as if it was a
formula) instead:

Function Near(Actual)
Near = Actual * Sheets("hiden").evaluate(Sheets("hiden").Cells(1,
1).formula)
End Function

You could simplify this by putting the formula directly inside the
evaluate()

Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com/FxlV2WhatsNew.htm

"Rob" wrote in message
...
I've defined a function in VBA to randomly generate a
number "close to" its argument.
It uses a formula on a hiden sheet, which gives a random
number between 0.666 and 1.333 to multiply the argument by.

Function Near(Actual)
Sheets("hiden").Cells.Calculate
Near = Actual * Sheets("hiden").Cells(1, 1)
End Function

Sub test()
Sheets("hiden").Cells(1, 1) _
.FormulaR1C1 = "=(rand()-0.5)/3+1"
MsgBox Near(100)
End Sub

The test procedure above works, but if I enter the
formula "=near(100)" into a cell on my worksheet and fill
the formula down a few rows, it doesn't calculate the
values. Does anyone know why this is?



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
cant calculate sum function Neeraj Paul Excel Discussion (Misc queries) 9 May 31st 09 06:09 PM
Calculate function Xx+1=rX(1-X). How? Kanon Excel Discussion (Misc queries) 2 March 19th 07 08:01 PM
function doesn't calculate [email protected] Excel Worksheet Functions 3 December 18th 06 04:26 AM
Function Won't Calculate -- Sometimes Bill Martin -- (Remove NOSPAM from address) Excel Discussion (Misc queries) 4 April 13th 05 04:38 PM
How to calculate PMT function Daniel Excel Worksheet Functions 1 November 18th 04 08:42 PM


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

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"