我试图简单地将两个范围相乘,并且它由一个activeX按钮激活,但它一直给我#Name?错误从乘法错误obv但我不知道如何正确编辑。 感谢
Private Sub CommandButton1_Click()
Dim rngData As Range
Dim rngNum As Range
Set rngData = ThisWorkbook.Worksheets("Custom Systems").Range("G16")
Set rngNum = ThisWorkbook.Worksheets("Custom Systems").Range("c8")
rngData = Evaluate(rngData.Address & "* rngNum")
End Sub
答案 0 :(得分:4)
您问题的最简单解决方案是:
With ThisWorkbook.Worksheets("Custom Systems")
.Range("G16").Value = .Range("G16").Value * .Range("c8").Value
End With
或
With ThisWorkbook.Worksheets("Custom Systems")
Set RngData = .Range("G16")
Set RngNum = .Range("c8")
RngData.Value = RngData.Value * RngNum.Value
End With
(.Value
是默认属性,因此如果要在键入时保存一些字符,可以省略。)
如果RngData
范围不是单个单元格,则上述方法无法正常工作。在那种情况下,我会迭代每个单元格,例如
Dim oneCell As Range
With ThisWorkbook.Worksheets("Sheet1")
Set rngdata = .Range("G11:G27")
Set rngnum = .Range("C6")
For Each oneCell In rngdata
oneCell.Value = oneCell.Value * rngnum.Value
Next
End With
或者,您可以回退使用内置的“粘贴特殊值乘法”方法,例如
With ThisWorkbook.Worksheets("Sheet1")
Set rngdata = .Range("G11:G27")
Set rngnum = .Range("C6")
rngnum.Copy
rngdata.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlMultiply
End With
或者你可以在那种情况下使用Evaluate
函数:
With ThisWorkbook.Worksheets("Sheet1")
Set rngdata = .Range("G11:G27")
Set rngnum = .Range("C6")
'Note: The ".Value" is required in the next line or it will not work
rngdata.Value = Evaluate(rngdata.Address & "*" & rngNum.Address)
End With
就个人而言,我不喜欢Evaluate
方法 - 我怀疑它比其他方法效率低。
答案 1 :(得分:2)
With ThisWorkbook.Worksheets("Custom Systems")
.[G11:G27] = .[G11:G27*C6]
End With