这是我根据下面提供的帮助更新的尝试。我想确保这将正确计算所有 6 个评论场景。
Sub recalc()
Dim adjValue As String
Dim runningTotal As Double
runningTotal = 0
'if user chooses 1 job to adjust or remove
If opt1Req Then
If optReduceReq Then
'reduce $ from one job
runningTotal = runningTotal + CDbl(txtAdjustmentAmt)
ElseIf optRemoveReq Then
'remove total $ from one job
runningTotal = runningTotal + CDbl(txtBudgetImpact)
End If
'if user chooses 2 jobs to adjust or remove
ElseIf opt2Reqs Then
If optReduceReq_2 Then
If optRemoveReq Then
'remove total $ from 1st job // reduce $ from 2nd job
runningTotal = runningTotal + CDbl(txtBudgetImpact) + CDbl(txtAdjustmentAmt_2)
Else
'reduce $ from 1st job // reduce $ from 2nd job
runningTotal = runningTotal + CDbl(txtAdjustmentAmt) + CDbl(txtAdjustmentAmt_2)
End If
ElseIf optRemoveReq_2 Then
If optRemoveReq Then
'remove total $ from 1st and 2nd job
runningTotal = runningTotal + CDbl(txtBudgetImpact) + CDbl(txtBudgetImpact_2)
Else
'reduce $ from 1st job // remove total $ from 2nd job
runningTotal = runningTotal + CDbl(txtAdjustmentAmt) + CDbl(txtBudgetImpact_2)
End If
End If
End If
adjValue = CDbl(runningTotal)
txtFunctionExcess.Value = FormattedRemainingBudget( _
txtBudget.Value, adjValue)
End If
End Sub
Function FormattedRemainingBudget(budget As String, adjustment As String) As String
Dim dblBudget As Double: dblAdjust = CDbl(budget)
Dim dblAdjust As Double: dblAdjust = CDbl(adjust)
FormattedRemainingBudget = Format(dblBudget - dblAdjust, "$#,##.00")
End Function
这是我第一次尝试理解更复杂的函数类型,所以非常感谢您的帮助!
答案 0 :(得分:1)
编辑
下面的代码将创建一个运行总值,然后传递给函数。请注意,我没有测试下面的代码(因为我没有具有相同文本框/控件名称的表单)。希望它为您提供了正确的方向,但如果您遇到问题,请回信。此外,我在代码中添加了注释,以或多或少地解释我在做什么。如果您需要更深入地了解这些,也请回信。我没有更改 FormattedRemainingBudget
函数,因此您可以保持原样。
Sub recalc()
Dim adjValue As String
Dim runningTotal As Double
'Initialize running total to 0
runningTotal = 0
If opt1Req Then 'if user chooses 1 job
If optReduceReq Then
runningTotal = runningTotal + CDbl(txtAdjustmentAmt.Value)
ElseIf optRemoveReq Then
runningTotal = runningTotal + CDbl(txtBudgetImpact.Value)
End If
ElseIf opt2Reqs Then
If optReduceReq_2 Then
runningTotal = runningTotal + CDbl(txtAdjustmentAmt_2.Value)
ElseIf optRemoveReq_2 Then
runningTotal = runningTotal + CDbl(txtBudgetImpact_2.Value)
End If
End If
'Since the FormattedRemainingBudge function expects
'the adjustment as a string, we can either convert
'running total to a string, or we can change the function
'to expect a Double. I'd recommend changing the function
'to accept a double since it will convert the string to a
'Double anyway. However, if that function is used in other
'places, and you change it, it might break code elsewhere.
'Below I'm going to convert the double to a string, but the
'option is yours based on the potential use cases.
adjValue = Str(runningTotal)
txtFunctionExcess.Value = FormattedRemainingBudget( _
txtBudget.Value, adjValue) 'departments excess $
End If
End Sub
无需重复 Function FormattedRemainingBudget
函数。它们都做同样的事情,但变量名不同。
这两个子程序 Recalc
和 Recalc_v2
本质上也做同样的事情。您可以将其重写为:
Sub recalc()
Dim adjValue as String
If opt1Req Then 'if user chooses 1 job
if optReduceReq then
adjValue = txtAdjustMentAmt.Value
elseif optRemoveReq then
adjValue = txtBudgetImpact.Value
end if
txtFunctionExcess.Value = FormattedRemainingBudget( _
txtBudget.Value, adjValue) 'departments excess $
End If
End If
End Sub