跨多行 Excel VBA 的乘法

  • 本文关键字:VBA Excel 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的行,并为每一行将第N列和L中的值的乘积写入第O列,然后将第O列中的(新)值与FxRate除以,并将除法结果写入第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

最新更新