简体   繁体   English

VBA列之间的数值差异

[英]Numerical difference between columns VBA

I have an excel file with 15000 rows of data.我有一个包含 15000 行数据的 excel 文件。 I have numerical values in Columns D & E .我在 D & E 列中有数值。 I have to calculate the difference between D & E and store it in F using VBA.我必须计算 D & E 之间的差异并使用 VBA 将其存储在 F 中。 I am new to VBA Help is appreciated.我是 VBA 的新手帮助表示赞赏。

Is it absolutely necessary that you use VBA for this ?您是否绝对有必要为此使用 VBA? If I understand the question correctly this would be much easier to do with excel formulas rather than using an array and having to loop through.如果我正确理解了这个问题,那么使用 excel 公式会更容易,而不是使用数组并且必须循环。

In cell F1 you need to type the following formula: = D1-E1在单元格F1您需要键入以下公式: = D1-E1

Be sure to include the equals sign.确保包含等号。 Then press enter.然后按回车。 Double click the bottom right corner of cell in F1 and it should auto fill all the way down.F1双击单元格的右下角,它应该会自动填充到底。

Hope this helps.希望这可以帮助。 This is my first post so hopefully I haven't done anything wrong !这是我的第一篇文章,所以希望我没有做错任何事!

For data between rows 2 and 15000, run:对于第 2 行和第 15000 行之间的数据,运行:

Sub SplitTheDifference()
    Range("F2:F15000").Formula = "=D2-E2"
End Sub

The formulas adjust as we move down the columns当我们向下移动列时,公式会调整

If we need values rather than formulas in column F , use:如果我们需要F列中的值而不是公式,请使用:

Sub SplitTheDifference()
    With Range("F2:F15000")
        .Formula = "=D2-E2"
        .Value = .Value
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM