代码之家  ›  专栏  ›  技术社区  ›  Chris S

VBA总和范围,包括变量

  •  0
  • Chris S  · 技术社区  · 9 年前

    所以我已经将myValue定义为输入值,但现在我想将输入值与F39:41的和相加。

    Dim myValue As Variant
    
        myValue = InputBox("Enter Value", "Enter Value in £'s", "£")
        Range("B7").Formula = "=Sum(F39:F41)" + CInt(myValue)
    

    范围(“B7”)。Value=myValue工作得很好,但如果我在公式中也包含和F39:41,它就失败了。我已经用CInt尝试过这个公式,但仍然无效。有什么想法吗?

    谢谢

    4 回复  |  直到 9 年前
        1
  •  2
  •   Rory    9 年前

    要输入公式,请使用:

    Range("B7").Formula = "=Sum(F39:F41)+" & CInt(myValue)
    
        2
  •  0
  •   Lobsterpants    9 年前

    我让你想:

    Dim myValue As Variant
    
    myValue = InputBox("Enter Value", "Enter Value in £'s", "£")
    Range("B7").Formula = "=Sum(F39:F41)  +" & CInt(myValue)
    
        3
  •  0
  •   Florent B.    9 年前

    InputBox返回的值是一个字符串,因此无需转换为整数即可将其添加到公式中。此外,您可能需要检查用户是否没有取消输入:

    Dim userInput As String
    userInput = InputBox("Enter Value", "Enter Value in £'s", "£")
    If Len(userInput) Then
      Range("B7").Formula = "=Sum(F39:F41)+" & userInput
    End If
    
        4
  •  -2
  •   Sixthsense    9 年前
    Range("B7").Value = evaluate("=Sum(F39:F41)") + CInt(myValue)