用结果有效地填充列



我正在尝试使用变量"ReportedPipeline"填充AO列(从AO2到最后一行(,该变量是根据同一行(O,P,T,U,W(列中的值计算的。

虽然我已经能够计算"报告管道",但我正在努力有效地将值填充到最后一行。

我创建了一个循环,该循环在AO2中填充一个值,然后将活动单元格向下移动1行,尽管效率非常低。我已经看过"如何避免在Excel VBA中使用选择"文章,但无法弄清楚如何应用它。

我使用的变量:

Dim oppstart As Date            'actual opportunity start
Dim oppend As Date              'actual opportunity end
Dim confidence As Integer       'opportunity confidence
Dim outstanding As Single       'outstanding amount
Dim status As String            'opportunity status - Lost or Open
Dim thisyear As Integer         'value of this year
Dim nextyear As Integer         'value of next year
Dim ReportedPipeline As Single  '(Outstanding amount / The subs' length in months)* months of this sub in this year
Dim monthsdiff As Integer       'How long the full length of the subscription is in months
Dim monthslength As Integer     'Length in months of subscription this year
status = Range("w" & ActiveCell.Row).Value
oppstart = Range("t" & ActiveCell.Row).Value
oppend = Range("u" & ActiveCell.Row).Value
confidence = Range("p" & ActiveCell.Row).Value
outstanding = Range("o" & ActiveCell.Row).Value 

如果有帮助,我可以分享完整的代码。

谢谢!

你最好循环浏览这些行,如下所示:

i = 2 to Range("O" & Rows.Count).End(xlUp).Row
    status = Range("w" & i).Value
    oppstart = Range("t" & i).Value
    oppend = Range("u" & i).Value
    confidence = Range("p" & i).Value
    outstanding = Range("o" & i).Value 
    'Do you calculation here
    Range("AO" & i).Value = Foo 'Foo is the result of you calculation
Next i

为了加快速度,您还可以使用:

Application.ScreenUpdating = False

在子的开头

也就是说,如果可以使用公式进行计算,则最好在第 AO 列中包含公式

与其循环浏览一个范围 - 当数据在 Excel 和 VBA 之间传输时,这会增加循环的每次迭代开销 - 只需使用 VBA 在 Excel 中的列中填充公式(需要一次传输(或使用变体数组一次性将数据引入 VBA 会更有效, 在 VBA 中迭代数组,然后一次性将其发送回 Excel。

最新更新