Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Circular reference in an open workbook

I get the error message:

"Microsoft Office Excel cannot calculate a formula. There is a circular
reference in an open workbook, but the references that cause it cannot be
listed for you. Try editing the last formula you entered or removing it with
the Undo command (Edit menu)."

This occurs when I set the formula in a cell equal to a UDF (shown below).
This is the only cell in the workbook with a formula.

If I remove the commented lines

'Range("ServiceYears").Select
'ActiveCell.Value = CalculatedYearsOfService

and execute the UDF from a user defined subroutine (i.e. a Macro) then
everything works fine. However, running a Macro every time another value in
the workbook changes isn't a very elegant solution.

What is really causing the error message?

=========================================
Public Function CalculatedYearsOfService() As Double

Dim NumberOfMonths As Integer
Dim NumberOfYears As Integer
Dim myServiceDate As Date
Dim myRetirementDate As Date

'Application.Volatile
'Application.EnableEvents = False

Excel.Range("ServiceDate").Select
myServiceDate = Excel.ActiveCell.Value
Excel.Range("RetirementDate").Select
myRetirementDate = Excel.ActiveCell.Value
CalculatedYearsOfService = (myRetirementDate - myServiceDate) / 365.25
NumberOfYears = Int(CalculatedYearsOfService)
NumberOfMonths = Int(12 * (CalculatedYearsOfService - NumberOfYears))
CalculatedYearsOfService = CDbl(NumberOfYears + NumberOfMonths / 12)
'Range("ServiceYears").Select
'ActiveCell.Value = CalculatedYearsOfService

'Application.EnableEvents = True

End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Circular reference in an open workbook

Sorry, I meant uncomment the commented lines so that the following lines
appear in the code:

Range("ServiceYears").Select
ActiveCell.Value = CalculatedYearsOfService


"Joe Cletcher" wrote:

I get the error message:

"Microsoft Office Excel cannot calculate a formula. There is a circular
reference in an open workbook, but the references that cause it cannot be
listed for you. Try editing the last formula you entered or removing it with
the Undo command (Edit menu)."

This occurs when I set the formula in a cell equal to a UDF (shown below).
This is the only cell in the workbook with a formula.

If I remove the commented lines

'Range("ServiceYears").Select
'ActiveCell.Value = CalculatedYearsOfService

and execute the UDF from a user defined subroutine (i.e. a Macro) then
everything works fine. However, running a Macro every time another value in
the workbook changes isn't a very elegant solution.

What is really causing the error message?

=========================================
Public Function CalculatedYearsOfService() As Double

Dim NumberOfMonths As Integer
Dim NumberOfYears As Integer
Dim myServiceDate As Date
Dim myRetirementDate As Date

'Application.Volatile
'Application.EnableEvents = False

Excel.Range("ServiceDate").Select
myServiceDate = Excel.ActiveCell.Value
Excel.Range("RetirementDate").Select
myRetirementDate = Excel.ActiveCell.Value
CalculatedYearsOfService = (myRetirementDate - myServiceDate) / 365.25
NumberOfYears = Int(CalculatedYearsOfService)
NumberOfMonths = Int(12 * (CalculatedYearsOfService - NumberOfYears))
CalculatedYearsOfService = CDbl(NumberOfYears + NumberOfMonths / 12)
'Range("ServiceYears").Select
'ActiveCell.Value = CalculatedYearsOfService

'Application.EnableEvents = True

End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Circular reference in an open workbook

You can't change anything in a worksheet from within a function that is
called from a worksheet. You can from a sub. You can't cheat Excel by
calling the sub from a function etc.

Change your function to accept the two dates as arguments:

Public Function CalculatedYearsOfService(myServiceDate As Date,
myRetirementDate As Date) As Double

Dim NumberOfMonths As Integer
Dim NumberOfYears As Integer

'Application.Volatile
'Application.EnableEvents = False

CalculatedYearsOfService = (myRetirementDate - myServiceDate) / 365.25
NumberOfYears = Int(CalculatedYearsOfService)
NumberOfMonths = Int(12 * (CalculatedYearsOfService - NumberOfYears))
CalculatedYearsOfService = CDbl(NumberOfYears + NumberOfMonths / 12)

'ActiveCell.Value = CalculatedYearsOfService
'Application.EnableEvents = True

End Function


--
Kind regards,

Niek Otten


"Joe Cletcher" <Oak Ridge National Laboratory wrote in message
...
Sorry, I meant uncomment the commented lines so that the following lines
appear in the code:

Range("ServiceYears").Select
ActiveCell.Value = CalculatedYearsOfService


"Joe Cletcher" wrote:

I get the error message:

"Microsoft Office Excel cannot calculate a formula. There is a circular
reference in an open workbook, but the references that cause it cannot be
listed for you. Try editing the last formula you entered or removing it
with
the Undo command (Edit menu)."

This occurs when I set the formula in a cell equal to a UDF (shown
below).
This is the only cell in the workbook with a formula.

If I remove the commented lines

'Range("ServiceYears").Select
'ActiveCell.Value = CalculatedYearsOfService

and execute the UDF from a user defined subroutine (i.e. a Macro) then
everything works fine. However, running a Macro every time another value
in
the workbook changes isn't a very elegant solution.

What is really causing the error message?

=========================================
Public Function CalculatedYearsOfService() As Double

Dim NumberOfMonths As Integer
Dim NumberOfYears As Integer
Dim myServiceDate As Date
Dim myRetirementDate As Date

'Application.Volatile
'Application.EnableEvents = False

Excel.Range("ServiceDate").Select
myServiceDate = Excel.ActiveCell.Value
Excel.Range("RetirementDate").Select
myRetirementDate = Excel.ActiveCell.Value
CalculatedYearsOfService = (myRetirementDate - myServiceDate) /
365.25
NumberOfYears = Int(CalculatedYearsOfService)
NumberOfMonths = Int(12 * (CalculatedYearsOfService - NumberOfYears))
CalculatedYearsOfService = CDbl(NumberOfYears + NumberOfMonths / 12)
'Range("ServiceYears").Select
'ActiveCell.Value = CalculatedYearsOfService

'Application.EnableEvents = True

End Function



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
excel 07, i don't want circular reference arrows every time I open Zack H Excel Discussion (Misc queries) 1 November 19th 09 08:56 PM
UDF not returning proper value - circular reference/multi workbook Jeff Setting up and Configuration of Excel 1 February 1st 06 12:52 AM
UDF not returning proper value - circular reference/multi workbook Jeff Excel Programming 1 February 1st 06 12:52 AM
Open an existing workbook and reference it TimT Excel Programming 1 October 12th 05 05:11 PM
removal of circular reference error on excel workbook load Nitin Mahadar Excel Programming 1 June 24th 05 02:01 PM


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

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"