繁体   English   中英

多行乘法Excel VBA

[英]Multiplication across multiple Rows Excel VBA

在 VBA 编程方面,我有点菜鸟。 我有两个基本的乘法公式,我想将它们应用于表中的所有行。 我如何动态编码它以便它为表中的每一行向下复制(行数每次都可能改变)? 而不是仅仅按顺序和逐行递增地复制此代码....

Dim FxRate As Variant

FxRate = InputBox("Input FX Rate:")

Range("O2").Select
  ActiveCell.Value = Range("N2") * Range("L2")

Range("P2").Select
  ActiveCell.Value = Range("O2") / FxRate

计算并写入单元格

  • 这是五个版本的中间解决方案,它们都做相同的事情。 它们按可读性降序排序(有些人可能认为第二个解决方案最易读,但第一个肯定是最易于维护的。)。 第一个展示了在使用常量时如何快速找到例如Res2 (第二个结果列)并将其更改为Z (假设有数百行。)。

  • 简而言之,代码首先打开一个InputBox ,用户应该在其中输入一个数值 ( FxRate )。 然后它计算N列中的最后一行(最后一个非空白单元格的行)。 然后,它循环通过从行FirstRowLastRow和为每一行中写入在列中的值的乘积NLO然后除以在列中的(新)值OFxRate并写入除法的结果列P 最后,使用MsgBox通知用户它已经完成(几乎)。

编码

Option Explicit

' Change the name of the procedure ("spreadData") to something more meaningful.
Sub spreadData()

    ' Constants
    ' Use common sense to decide which column to use to calculate the last row.
    Const LastRowCol As Variant = "N" ' e.g. 1 or "A"
    Const FirstRow As Long = 2
    ' Find/Replace the following 4 constants names with descriptive ones
    ' like you did with "FxRate".
    Const Col1 As Variant = "N"
    Const Col2 As Variant = "L"
    Const Res1 As Variant = "O"
    Const Res2 As Variant = "P"
    
    ' Let the user input a value.
    Dim FxRate As Variant
    FxRate = InputBox("Input FX Rate:")
    
    ' Calculate the Last Row containing data in Last Row Column.
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, LastRowCol).End(xlUp).Row
    
    ' Calculate and write the results to cells.
    Dim i As Long
    For i = FirstRow To LastRow
        Cells(i, Res1).Value = Cells(i, Col1).Value * Cells(i, Col2).Value
        Cells(i, Res2).Value = Cells(i, Res1).Value / FxRate
    Next i

    ' Inform user.
    MsgBox "Data written.", vbInformation, "Success"

End Sub

Sub spreadDataNoConstants()
    
    ' Let the user input a value.
    Dim FxRate As Variant
    FxRate = InputBox("Input FX Rate:")
    
    ' Calculate the Last Row containing data in Last Row Column.
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "N").End(xlUp).Row
    
    ' Calculate and write the results to cells.
    Dim i As Long
    For i = 2 To LastRow
        Cells(i, "O").Value = Cells(i, "N").Value * Cells(i, "L").Value
        Cells(i, "P").Value = Cells(i, "O").Value / FxRate
    Next i

    ' Inform user.
    MsgBox "Data written.", vbInformation, "Success"

End Sub

Sub spreadDataNoComments()
    
    Dim FxRate As Variant
    FxRate = InputBox("Input FX Rate:")
    
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "N").End(xlUp).Row
    
    Dim i As Long
    For i = 2 To LastRow
        Cells(i, "O").Value = Cells(i, "N").Value * Cells(i, "L").Value
        Cells(i, "P").Value = Cells(i, "O").Value / FxRate
    Next i

    MsgBox "Data written.", vbInformation, "Success"

End Sub

Sub spreadDataNoSections()
    Dim FxRate As Variant
    FxRate = InputBox("Input FX Rate:")
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "N").End(xlUp).Row
    Dim i As Long
    For i = 2 To LastRow
        Cells(i, "O").Value = Cells(i, "N").Value * Cells(i, "L").Value
        Cells(i, "P").Value = Cells(i, "O").Value / FxRate
    Next i
    MsgBox "Data written.", vbInformation, "Success"
End Sub

Sub spreadDataNoIndentation()
Dim FxRate As Variant
FxRate = InputBox("Input FX Rate:")
Dim LastRow As Long
LastRow = Cells(Rows.Count, "N").End(xlUp).Row
Dim i As Long
For i = 2 To LastRow
Cells(i, "O").Value = Cells(i, "N").Value * Cells(i, "L").Value
Cells(i, "P").Value = Cells(i, "O").Value / FxRate
Next i
MsgBox "Data written.", vbInformation, "Success"
End Sub

暂无
暂无

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

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