我只是在学习使用VBA,并且通过用户窗体输入为计算创建了以下代码:
我的困难是确保VarCost
,SemiVarCost
和Desired Margin
的计算是可选的。
也就是说,如果用户在userform文本字段中输入特定值,则会进行计算。如果不是,则该过程应将其计为0
或null
。
对于期望的保证金,如果用户输入了OKMargin = DesiredMargin
,则为DesiredMargin
。
有人可以帮我吗?
Private Sub Calculate_Click()
Dim PIS, Cofins, ISS, Simples, LR, IRRJ, OKMargin As Double
Dim GCom, eLComNet, ClientCom As Double
Dim LucroReal, SimplesN, TaxationType As String
Dim OptimalCommission As Currency
Dim VarCost, SemiVarCost, DesiredMargin As Currency
Dim ClientSpent As Currency
'Ref the userform
TaxationType = UserForm1.ComboBox1.Text
SemiVarCost = UserForm1.SemiVarCost.Value
VarCost = UserForm1.VarCost.Value
ClientSpent = UserForm1.ClientSpent.Value
DesiredMargin = UserForm1.DesiredMargin.Value
' Tax assumptions behind the calculations
PIS = 0.0165
Cofins = 0.076
ISS = 0.05
IRRJ = 0.34
Simples = 0.062
LR = PIS + Cofins + ISS + IRRJ
GCom = 0.12
OKMargin = 0.55
'define taxation conditions
If (TaxationType = "SimplesN") And (IsMissing(DesiredMargin)) Then
eLComNet = (ClientSpent * GCom) - (ClientSpent * GCom * Simples)
OptimalCommission = eLComNet - (eLComNet * OKMargin) - VarCost - SemiVarCost
End If
If (TaxationType = "LucroReal") And (IsMissing(DesiredMargin)) Then
eLComNet = (ClientSpent * GCom) - (ClientSpent * GCom * LR)
OptimalCommission = eLComNet - (eLComNet * OKMargin) - VarCost - SemiVarCost
End If
If (TaxationType = "SimplesN") And (Not IsMissing(DesiredMargin)) Then
eLComNet = (ClientSpent * GCom) - (ClientSpent * GCom * Simples)
OptimalCommission = eLComNet - (eLComNet * DesiredMargin) - VarCost - SemiVarCost
End If
If (TaxationType = "LucroReal") And (Not IsMissing(DesiredMargin)) Then
eLComNet = (ClientSpent * GCom) - (ClientSpent * GCom * LR)
OptimalCommission = eLComNet - (eLComNet * DesiredMargin) - VarCost - SemiVarCost
End if
UserForm1.OptCom.Value = OptimalCommission
End Sub