[英]In Excel and VBA, how to set value of cell to the formulas result rather than the formula
I'm getting values from one sheet and placing them in another using a macro in Excel. 我从一张纸上获取值,并使用Excel中的宏将它们放在另一张纸上。 I currently have this which works fine: 我目前有这个工作正常:
sheet.range("B2:B35").Value = "=IF(SUMPRODUCT(--(Raw!$B$11:$B$322=$A2),--(Raw!$D$11:$D$322=All!$B$2),Raw!$H$11:$H$322)<>0,SUMPRODUCT(--(Raw!$B$11:$B$322=$A2),--(Raw!$D$11:$D$322=All!$B$2),Raw!$H$11:$H$322),""-"")"
It, obviously, puts that entire formula as the value of the cell. 显然,它将整个公式作为单元格的值。 What I'd like is it just to put the result of the formula into the cell. 我想要的只是将公式的结果放入单元格中。 I've tried adding Evaluate() around the "IF..." part, but then the IF doesn't evaluate correctly (I just end up with "-" in each cell). 我已经尝试在“IF ...”部分周围添加Evaluate(),但是IF没有正确评估(我在每个单元格中都以“ - ”结尾)。 Is this possible to do or do I have to have separate code to loop through and change the value to the value of the cell? 这是可能做的还是我必须有单独的代码循环并将值更改为单元格的值?
Use: 采用:
sheet.range("B2:B35").Formula = "Your formula here"
If that doesn't work you may have to change the formatting (do this first): 如果这不起作用,您可能需要更改格式(首先执行此操作):
sheet.range("B2:B35").NumberFormat = "General"
Edit: 编辑:
A solution turned out to be addition of the following line after the OP's code: 在OP的代码之后,解决方案结果是添加了以下行:
sheet.range("B2:B35").value = sheet.range("B2:B35").value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.