UDF Calculation Issue - Cells don't "hold" value

jackdemo

New Member
Joined
Sep 19, 2011
Messages
2
Hello,

Using Excel 2007 on Windows 7. I've created a function to calculate a total based on a number of factors. The function calculates the right answer and works fine within a given worksheet; however, when I pop between sheets, I receive a #Value error, and it requires a recalculation (F9) to refresh. I need the cell to "hold" the value.

Thanks in advance for your help! Here's the vba code.


Function GetTotalDollars(ar As Integer, ac As Integer, SheetName As String) ' As String
'Application.Volatile
'Debug.Print ar
'Debug.Print ac
'ActiveSheet.EnableCalculation = False
'ActiveSheet.EnableCalculation = True
Dim ColVal(1, 3)
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim Rng4 As Range
Dim Rng5 As Range
Dim Rng6 As Range
Dim curCell1 As Variant
Dim curCell2 As Variant
Dim curCell3 As Variant
Dim DaysPerWeek As Integer
Dim HoursPerDay As Integer
Dim DaysPerMonth As Integer
Dim DollarsSpent As Double
Dim DollarsSpentSum As Double
Dim Proj As String
Dim ProjPLC As String
Dim ControlSheet As String
Dim iCount As Integer
Dim Max As Integer
'Dim ac As Integer
'Dim ar As Integer
ControlSheet = "forecast_control_sheets.xlsm"
Set Rng1 = ActiveWorkbook.Worksheets(SheetName).Range("A6:D25")
Set Rng2 = ActiveWorkbook.Worksheets(SheetName).Range("B6:D25")
Set Rng3 = ActiveWorkbook.Worksheets(SheetName).Range(Cells(6, ac), Cells(25, ac))
Set Rng4 = Workbooks(ControlSheet).Names("ProjectPLCKeyRange").RefersToRange
Set Rng5 = Workbooks(ControlSheet).Names("PLCRatesOY_DataRange").RefersToRange
Set Rng6 = Workbooks(ControlSheet).Names("BillRateYearHeaderRange").RefersToRange

DaysPerWeek = Sheets("forecast").Range("D26").Value
HoursPerDay = Sheets("forecast").Range("D27").Value
DollarsSpent = 0
DollarsSpentSum = 0
Proj = Sheets(SheetName).Range("$A$1").Value
Max = Sheets(SheetName).Range("A6:D25").Rows.Count - 1
'ReDim ColVal(1 To Max)
'Debug.Print ("row= " & ar)
'Debug.Print ("column= " & ac)

For iCount = 1 To Max
DaysPerMonth = Sheets(SheetName).Cells(5, ac)

Set curCell1 = Rng1(iCount, 1)
Set curCell2 = Rng2(iCount, 1)
Set curCell3 = Rng3(iCount, 1)

ColVal(1, 1) = curCell1
ColVal(1, 2) = curCell2
ColVal(1, 3) = curCell3

ProjPLC = Proj & "-" & ColVal(1, 2)
'Debug.Print (ProjPLC)
'Debug.Print ColVal(1, 1)
'Debug.Print ColVal(1, 2)
'Debug.Print ColVal(1, 3)

If ColVal(1, 3) > 0 Then
'Debug.Print ColVal(1, 3)
'Debug.Print (Cells(2, ac).Value)

MatchVal1 = WorksheetFunction.Match(ProjPLC, Rng4, 0)
MatchVal2 = WorksheetFunction.Match(Sheets(SheetName).Cells(2, ac), Rng6, 0)
IndexVal1 = WorksheetFunction.Index(Rng5, MatchVal1, MatchVal2)

If SheetName = "forecast" Then
DollarsSpent = IndexVal1 * HoursPerDay * DaysPerMonth * ColVal(1, 3)
ElseIf SheetName = "Burn" Then
DollarsSpent = IndexVal1 * ColVal(1, 3)
End If

DollarsSpentSum = DollarsSpentSum + DollarsSpent

'Debug.Print (MatchVal1)
'Debug.Print (MatchVal2)
'Debug.Print (IndexVal1)
'Debug.Print (DollarsSpent)
'Debug.Print (DollarsSpentSum)
'Debug.Print (iCount)

End If

Next iCount

Debug.Print (DollarsSpentSum)
GetTotalDollars = DollarsSpentSum

Erase ColVal()

End Function
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top