在我的 Excel 表格中,一行(第 5 行)作为商品数量,另一行(第 6 行)作为商品价格。例如,我想将 200 乘以 56.50 美元,但我在使用此脚本时遇到问题。任何人都可以帮忙吗?
Sub calcprice()
Dim i As Integer
Dim iRowNumber As Integer ' Integer to store result in
Dim val As Double
iRowNumber = InputBox(Prompt:="Number of Rows", _
Title:="Rows: ", Default:="# of Rows")
For i = 1 To iRowNumber
If Cells(i, 5).Value >= 0 And Cells(i, 6).Value >= 0 And IsEmpty(Cells(i, 5)) = False And IsEmpty(Cells(i, 5)) = False Then
val = FormatCurrency(Cells(i, 5).Value) * Cells(i, 6).Value
Cells(i, 7).Value = val
End If
Next i
End Sub
它说运行时错误 13
类型不匹配
这是图像:
这是链接:https://www.dropbox.com/s/lla2cuz8hqu5qyp/test.xlsm
我也不能使用 =a*b 我必须使用宏!
最佳答案
你不需要循环
您可以使用 G 列中的单个镜头范围
- 将 G5 中的公式添加到用户输入的
iRowNumber
中,以测试每行中是否出现 > 0 的结果(或添加""
以获得 0 结果)< 用值覆盖公式
Sub calcprice() Dim iRowNumber As Long ' Integer to store result in iRowNumber = InputBox(Prompt:="Number of Rows", _ Title:="Rows: ", Default:="# of Rows") With Range(Cells(5, 7), Cells(iRowNumber, 7)) .FormulaR1C1 = "=IF(N(RC[-1]*RC[-2]),RC[-1]*RC[-2],"""")" .Value = .Value End With End Sub
关于excel - 在 Excel VBA 中将整数与货币相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210585/