有没有一种方法可以加快此代码的速度。我是VBA的新手(本周才真正开始),并且尝试编写此宏以根据财务模型自动计算需要借入的金额。
为了提供一些背景信息,此单元格会在另一个工作表上通知一个峰值借贷需求(pbr)单元,但是当您增加所需借贷价值(fr)时,pbr完全是由于利息和借入金额的各种其他费用。
我创建了一系列的while循环,以使fr值最接近10,000,但是速度非常慢。我敢肯定,必须有一种更优雅的方式来撰写本文,但我似乎无法弄清楚。最好是我想将其转换为函数而不是子函数,但是我什至不确定是否可行。
到目前为止,这里是代码,您能提供的任何帮助将不胜感激!
' Sub procedure to calculate the peak borrowing requirement'
Sub calculateFacilityRequiredButton()
Dim pbr As Long ' stores the initial peak borrowing requirement from the viability page
Dim fr As Long ' stores the facility required from the inputs page
' set pbr variable as the value from the viability page
Worksheets("Viability").Activate
pbr = Cells(9, "k").Value
' set the starting value at the current peak borrowing rate from the viability page
Worksheets("Viability").Activate
fr = Cells(9, "K").Value
Do While fr <= pbr
If fr <= pbr Then
fr = fr + 1000000
Worksheets("Inputs").Activate
Range("N47").Value = fr
Worksheets("Viability").Activate
pbr = Cells(9, "k").Value
End If
Loop
Do While fr > pbr + 100000
If fr > pbr + 100000 Then
fr = fr - 100000
Worksheets("Inputs").Activate
Range("N47").Value = fr
Worksheets("Viability").Activate
pbr = Cells(9, "k").Value
End If
Loop
Do While fr > pbr + 10000
If fr > pbr + 10000 Then
fr = fr - 10000
Worksheets("Inputs").Activate
Range("N47").Value = fr
Worksheets("Viability").Activate
pbr = Cells(9, "k").Value
End If
Loop
Worksheets("Inputs").Activate
End Sub
您的While
循环似乎具有与您的if
条件相同的条件。据我所知,它只会在您的While
循环中执行一次循环。因此,我认为您不需要while
循环。另外,正如其他人提到的那样,请勿尝试激活工作表。将您的工作表限定为:
Dim oW As Worksheet: Set oW = ThisWorkbook.Worksheets("Viability")
然后,您可以将其用作oW.Range("N47").Value = fr
。这应该为您指明正确的方向
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句