Multiply a range with a constant

2019-08-31 10:07发布

I want to multiply everything within the range by a value. I tried this format, but the values became way off.

Code :

Dim yl1 As Range

Set yl1 = .Range(.Cells(17, 6), .Cells(n + 16, 6))*19.25

3条回答
小情绪 Triste *
2楼-- · 2019-08-31 10:31

Another way:

Sub UsingPaste()
    Range("B1").Value = 0.7
    Range("B1").Copy
    n = 10
    Range(Cells(17, 6), Cells(n + 16, 6)).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply
End Sub

Where B1 is some un-used cell and n is whatever it happens to be.

查看更多
Bombasti
3楼-- · 2019-08-31 10:37

There are probably other ways, many of them probably better, but one is just a loop

For i = 17 to some last row
    Cells(i, 6) = Cells(i, 6) * 19.25
Next i

I find this way the most intuitive, though.

查看更多
仙女界的扛把子
4楼-- · 2019-08-31 10:40

As per the link:

Dim yl1 as Range
Set yl1 = .Range(.Cells(17, 6), .Cells(n + 16, 6))
yl1.Value = .Evaluate("INDEX(" & yl1.Address(0,0) & " * 19.25,)")

It will do them all at once.

查看更多
登录 后发表回答