excel - 在 Excel VBA 中将整数与货币相乘

标签 excel visual-studio-macros vba

在我的 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
类型不匹配 这是图像: it says currency



这是链接: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/

相关文章:

c++ - 在 VS2015 宏中找不到 VCCLCompilerTool(使用 Visual Commander)

c++ - 未限定范围的多行 MACROS 的危险

vba - 对于 range.formatconditions 中的每个 fc 都失败。为什么?

string - 使用单元格值作为字符串 VBA

excel - 通过 VBA 将 Excel 表写入 CSV

excel - 求和索引匹配

.net - 如何制作 Visual Studio 宏以将调试器附加到 w3wp.exe 的所有实例?

excel - 如何使用 Excel VBA 单击网页上的链接?

vba - 更改 Outlook(回复和全部回复)本身的行为,将原始电子邮件中的附件添加到回复的邮件中

java - LibreOffice UNO : Setting Styles (anwer can be provided in Java, VB、Python、C++,任何使用 UNO API 的语言)