0
我已經有四片材的工作簿:用戶定義函數計算弄亂
- 設置
- 產品A
- 產品B
- 概述
設置片包含變量,其被其他表單用於計算。
產品A和產品B表使用不同的UDF來計算增長收入和輟學率。我們使用不同的UDF,因爲每種產品的增長計算公式等都不相同。
概述介紹從產品A和產品B.
總計現在的問題如下。
當我計算sheet產品A時,它會混淆產品B的值,反之亦然。
任何想法?
編輯:
UDF
Private Sub Workbook_SelectionChange(ByVal Target As Range)
Calculate
End Sub
Public Function PSPGrowth(CurrentPos, DataColStart, CountryPSPNew, AvgDuration, Fee, Growth1, Growth2, Growth3, Growth4, Growth, CalcRange As Range)
'recalculate on cell changes
Application.Volatile True
'define variables
Sum = 0
PosCurrent = CurrentPos.Column()
PSPNewRow = CountryPSPNew.Row()
'calculate starting point
PosStart = PosCurrent - AvgDuration
If (PosStart <= DataColStart) Then
PosStart = DataColStart
End If
'calculate duration
Duration = 1 + PosCurrent - PosStart
'loop new contracts
For c = PosStart To PosCurrent
'get new contracts
NewContracts = Cells(PSPNewRow, c)
If (NewContracts > 0) Then
'get contract duration till current position
ContractDuration = 1 + PosCurrent - c
'calculate revenue growth according to contract duration
If (ContractDuration < 13) Then
'year 1
cDuration = ContractDuration
y1 = (1 + Growth1.Value/12)
Sum = Sum + (NewContracts * (1 + Growth1.Value/12))
ElseIf (ContractDuration > 12 And ContractDuration < 25) Then
'year 2
cDuration = ContractDuration - 12
y1 = (1 + Growth1.Value)
y2 = y1 * (1 + (cDuration * Growth2.Value/12))
Sum = Sum + (NewContracts * y2)
ElseIf (ContractDuration > 24 And ContractDuration < 37) Then
'year 3
cDuration = ContractDuration - 24
y1 = (1 + Growth1.Value)
y2 = y1 * (1 + Growth2.Value)
y3 = y2 * (1 + (cDuration * Growth3.Value/12))
Sum = Sum + (NewContracts * y3)
'Sum = Sum + (NewContracts * (1 + Growth1.Value + Growth2.Value + Growth3.Value/12))
ElseIf (ContractDuration > 36 And ContractDuration < 49) Then
'year 4
cDuration = ContractDuration - 36
y1 = (1 + Growth1.Value)
y2 = y1 * (1 + Growth2.Value)
y3 = y2 * (1 + Growth3.Value)
y4 = y3 * (1 + (cDuration * Growth4.Value/12))
Sum = Sum + (NewContracts * y4)
' Sum = Sum + (NewContracts * (1 + Growth1.Value + Growth2.Value + Growth3.Value + Growth4.Value/12))
ElseIf (ContractDuration > 48) Then
'year 5+
cDuration = ContractDuration - 48
y1 = (1 + Growth1.Value)
y2 = y1 * (1 + Growth2.Value)
y3 = y2 * (1 + Growth3.Value)
y4 = y3 * (1 + Growth4.Value)
y5 = y4 * (1 + (cDuration * Growth5.Value/12))
Sum = Sum + (NewContracts * y5)
'Sum = Sum + (NewContracts * (1 + Growth1.Value + Growth2.Value + Growth3.Value + Growth4.Value + Growth5.Value/12))
Else
Sum = Sum + 0
End If
End If
Next
'return result
PSPGrowth = Sum * Fee.Value
End Function
Public Function PRODUCTAActive(CurrentPos, DataColStart, CountryBSNew, DropOutMonth, DropOutRate, CalcRange As Range)
'recalculate on cell changes
Application.Volatile True
'define variables
Sum = 0
PosCurrent = CurrentPos.Column
BSNewRow = CountryBSNew.Row()
'calculate starting point
PosStart = DataColStart
If (PosStart <= DataColStart) Then
PosStart = DataColStart
End If
'calculate duration
Duration = 1 + PosCurrent - PosStart
'loop new contracts
For c = PosStart To PosCurrent
'get new contracts
NewContracts = Cells(BSNewRow, c)
If (NewContracts > 0) Then
'get contract duration till current position
ContractDuration = 1 + PosCurrent - c
'calculate revenue growth according to contract duration
If (ContractDuration <= DropOutMonth) Then
Sum = Sum + Round(NewContracts, 0)
Else
Sum = Sum + Round((NewContacts * (1 - DropOutRate)), 0)
End If
Else
Sum = Sum + 0
End If
Next
'return result
PRODUCTAActive = Sum
End Function
Public Function PRODUCTAConvertionCalc(CurrentPos, DataColStart, CountryBSNew, ConvertionMonth, ConvertionRate, CalcRange As Range)
'recalculate on cell changes
Application.Volatile True
'define variables
Sum = 0
PosCurrent = CurrentPos.Column
BSNewRow = CountryBSNew.Row()
'calculate starting point
PosStart = DataColStart
If (PosStart <= DataColStart) Then
PosStart = DataColStart
End If
'calculate duration
Duration = 1 + PosCurrent - PosStart
'loop new contracts
For c = PosStart To PosCurrent
'get new contracts
NewContracts = Cells(BSNewRow, c)
If (NewContracts > 0) Then
'get contract duration till current position
ContractDuration = 1 + PosCurrent - c
'calculate revenue growth according to contract duration
If (ContractDuration = ConvertionMonth) Then
Sum = Sum + Round(NewContracts * ConvertionRate, 0)
Else
Sum = Sum + 0
End If
Else
Sum = Sum + 0
End If
Next
'return result
PRODUCTAConvertionCalc = Sum
End Function
Public Function PRODUCTACommissionCalc(CurrentPos, DataColStart, CountryBSNew, DropOutMonth, DropOutRate, ComY1, ComY2, PremiumPrice, CountryBSNewConv, ConvertionMonth, ConvertionRate, CalcRange1 As Range, CalcRange2 As Range)
'recalculate on cell changes
Application.Volatile True
'define variables
Sum = 0
TotalCom = 0
PosCurrent = CurrentPos.Column
BSNewRow = CountryBSNew.Row()
'calculate starting point
PosStart = DataColStart
If (PosStart <= DataColStart) Then
PosStart = DataColStart
End If
'calculate duration
Duration = 1 + PosCurrent - PosStart
'loop new contracts
For c = PosStart To PosCurrent
'get new contracts
NewContracts = Cells(BSNewRow, c)
If (NewContracts > 0) Then
'get contract duration till current position
ContractDuration = 1 + PosCurrent - c
'calculate revenue from new contracts
If (ContractDuration <= DropOutMonth) Then
If (ContractDuration <= 12) Then
TotalCom = TotalCom + Round(NewContracts, 0) * ComY1 * PremiumPrice
Else
TotalCom = TotalCom + Round(NewContracts, 0) * ComY2 * PremiumPrice
End If
Else
If (ContractDuration <= 12) Then
TotalCom = TotalCom + Round((NewContacts * (1 - DropOutRate)), 0) * ComY1 * PremiumPrice
Else
TotalCom = TotalCom + Round((NewContacts * (1 - DropOutRate)), 0) * ComY2 * PremiumPrice
End If
End If
Else
TotalCom = TotalCom + 0
End If
Next
' converted contracts
BSNewRow = CountryBSNewConv.Row()
'loop new contracts
For c = PosStart To PosCurrent
'get new contracts
NewContracts = Cells(BSNewRow, c)
If (NewContracts > 0) Then
'get contract duration till current position
ContractDuration = 1 + PosCurrent - c
'calculate revenue growth according to contract duration
If (ContractDuration >= ConvertionMonth) Then
If (ContractDuration <= 12) Then
TotalCom = TotalCom + Round(NewContracts * ConvertionRate, 0) * ComY1 * PremiumPrice
Else
TotalCom = TotalCom + Round(NewContracts * ConvertionRate, 0) * ComY2 * PremiumPrice
End If
Else
TotalCom = TotalCom + 0
End If
Else
TotalCom = TotalCom + 0
End If
Next
'return result
PRODUCTACommissionCalc = TotalCom
End Function
請發佈UDF。我懷疑你有'Range','ActiveCell'或'Cells'的引用,這些引用默認爲活動表單。 – Rory
@Rory只包括UDF – curiusX
您是否嘗試過Worksheets(「SheetName」)。計算? –