跨多行的乘法Excel VBA
当涉及到VBA编程时,我有点不在行。我有两个基本的乘法公式,我想应用于表中的所有行。我如何动态地编写代码,以便它为表中的每一行进行复制(其中的行数可能每次都会更改)?而不仅仅是按顺序逐行递增地复制此代码跨多行的乘法Excel VBA,excel,vba,Excel,Vba,当涉及到VBA编程时,我有点不在行。我有两个基本的乘法公式,我想应用于表中的所有行。我如何动态地编写代码,以便它为表中的每一行进行复制(其中的行数可能每次都会更改)?而不仅仅是按顺序逐行递增地复制此代码 Dim FxRate As Variant FxRate = InputBox("Input FX Rate:") Range("O2").Select ActiveCell.Value = Range("N2") * Rang
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
到FirstRow
的行中循环,并为每一行将列LastRow
和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
使用公式<代码>范围(“O2:O100”)。公式=“=N2*L2”,根据需要将
100
更改为最后一行。@BigBen您不是指lastrow
变量吗?谢谢您的评论。。我想把结果的乘积四舍五入到两位小数。代码是什么样子的?