vba 使用excel VBA将A列的值乘以B列的值并得出C列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25272414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiply column A values to Column B values and result in column C using excel VBA
提问by Learner
I have two columns A and B. Range of A is A1:A6 and B is B1:B6. Now, I want to multiply these two columns and display result in column C using excel VBA. For example
我有两列 A 和 B。A 的范围是 A1:A6,B 是 B1:B6。现在,我想将这两列相乘,并使用 excel VBA 在 C 列中显示结果。例如
A B C
美国广播公司
2 3 6 (It is A1*B1)
2 3 6 (A1*B1)
3 8 24
3 8 24
9 2 18
9 2 18
7 3 21
7 3 21
2 4 8
2 4 8
5 4 20
5 4 20
I have tried this code but did not find it helpful:
我试过这段代码,但没有发现它有帮助:
Range("C1:C6").Value = Range("A1:A6") * Range("B1:B6")
Please guide me here as I am new to VBA. You help is really appreciated.
请在这里指导我,因为我是 VBA 的新手。你的帮助真的很感激。
回答by Matt Cremeens
Alternatively,
或者,
for i = 1 to 6
cells(i,3) = cells(i,1) * cells(i,2)
next i
回答by Siddharth Rout
Try this. I have commented the code so that you will not have a problem understanding it. But if you still do then simply ask :)
尝试这个。我已经对代码进行了注释,这样您就不会在理解它时遇到问题。但如果你仍然这样做,那么简单地问:)
'~~> This will enter the formula in the entire range in one go
Range("C1:C6").Formula = "=A1*B1"
'~~> This will convert the formula to values
Range("C1:C6").Value = Range("C1:C6").Value