Excel 有没有办法将多个函数截断为一个?
下面是我根据下面提供的帮助更新的尝试。我想确保这将正确地计算所有被评论的6个场景Excel 有没有办法将多个函数截断为一个?,excel,vba,Excel,Vba,下面是我根据下面提供的帮助更新的尝试。我想确保这将正确地计算所有被评论的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
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
这是我第一次尝试理解更复杂的函数类型,因此我感谢您的帮助
编辑
下面的代码将创建一个运行的总值,然后传递给函数。请注意,我没有测试下面的代码(因为我没有具有相同文本框/控件名称的表单)。希望它能给你正确的方向,但如果你遇到问题,请回信。此外,我在代码中添加了注释,或多或少地解释了我在做什么。如果你需要对这些问题有更多的了解,也请回信。我没有更改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
无需重复函数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
功能如何?我能保持原样吗?@NidenK是的,函数就这么好。所以我使用了两种场景(作业1调整,作业1删除),但我还有一些其他场景。编辑我的帖子以合并该更改。我添加了另一个场景。我发布的是一个基本示例,但是如果单击了
opt2Reqs
,那么我需要在计算中减去第一个作业中单击的内容(删除或重新调整),然后包括第二个作业中的哪些内容,然后从txtbudent
中减去这些内容。这有意义吗?@NidenK您可以建立一个变量来聚合总值。例如:Dim totalChanges为Double
然后如果opt1value。。。。totalChanges=totalChanges+txtOpt1.值。。。您可以对每个场景执行此操作,然后将其用作总调整值。然而,我会提到,当我看到这样的布局时,它通常是一个糟糕的设计。当您需要两个以上的请求时会发生什么情况?你将有5到10种不同的工作条件。我认为你应该考虑不同的应用设计。