VBA Worksheet Funtion Won't Recalculate

soteman2005

New Member
Joined
Nov 24, 2005
Messages
32
Hi,

I have created a function in VBA to use in my excel worksheets. It is an "if" function referring to a number on a different sheet i.e. =if(sheet2!H15=1,do this,do that). It works fine except that it won't recalculate when I change the number on the other sheet. I have to press shift + F9 in order for it to give me the correct value. I have tried application.volatile but it doesn;t have any effect. Any suggestions?

Thanks

Adam
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,402
Show the VBA please. And tell how exactly you tried Application.Volatile. I'm assuming calculation is set as Automatic.
 

soteman2005

New Member
Joined
Nov 24, 2005
Messages
32
Here is my code....namerange shift is another function that I have used to select cells in a named range that are to the left or right of the current column as for example, when using Excel's SUM function, excel tried to SUM the entire name range.

Code:
Function CalculateAverageSubscribersEOP(ByRef EOPSubscriberRange As Range, ByVal ThisCol As Double) As Double
  
Dim TestNumber As Boolean
Dim SubRangeValMinus1
Dim SubRangeVal
Dim SubRangeValPlus1

SubRangeValMinus1 = Range(EOPSubscriberRange.Address).Cells(1, ThisCol - 1).Value
SubRangeVal = Range(EOPSubscriberRange.Address).Cells(1, ThisCol).Value
SubRangeValPlus1 = Range(EOPSubscriberRange.Address).Cells(1, ThisCol + 1).Value

TestNumber = Application.WorksheetFunction.IsNumber(NameRangeShift(EOPSubscriberRange, -1, ThisCol))

If TestNumber Then
    CalculateAverageSubscribersEOP = (SubRangeVal + SubRangeValMinus1) / 2
Else
    CalculateAverageSubscribersEOP = (SubRangeVal * SubRangeVal / SubRangeValPlus1 + SubRangeVal) / 2
End If

End Function

Thanks
 

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,402
Tell how exactly you tried Application.Volatile. I'm assuming calculation is set as Automatic.
 

soteman2005

New Member
Joined
Nov 24, 2005
Messages
32
I used application.volatile straight after the function at the top of the code, before I dimensioned anything. Automatic calculation is turned on.

Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,118,530
Messages
5,572,675
Members
412,481
Latest member
nhantam
Top