简体   繁体   English

在Excel和VBA中,如何将单元格的值设置为公式结果而不是公式

[英]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.

相关问题 Excel VBA - 如何引用单元格的可见内容而不是其公式? - Excel VBA - How do I refer to a cell's visible content rather than its formula? 如何使用vlookup搜索单元格值而不是其单元格引用公式(Excel) - How to use vlookup to search for a cell value rather than its cell reference formula(Excel) 如何使用VBA在Excel单元格中设置公式? - How to set a formula in an Excel cell using VBA? Excel导出; 使用VBA,如何将单元格导出为格式而不是原始单元格值 - Excel export ; using VBA, how to export cell as formatted rather than the raw cell value Excel VBA如果单元格中的公式值小于零,则msgbox - Excel VBA if formula value in cell is less than zero then msgbox Excel VBA 模块在整列中插入公式,而不是在行中插入单个单元格 - Excel VBA Module is inserting formula in entire column rather than single cell in row Excel 2010+ VBA - 如何搜索范围的公式而不是值 - Excel 2010+ VBA - How can I Search a Range's Formulas Rather Than Values VBA-设置包含引号作为单元格值的Excel公式 - VBA - Set Excel formula containing quotation marks as cell value Python使用公式计算的值读取Excel单元格,但公式字符串本身不读取 - Python to read Excel cell with the value calculated by the formulas but not the formula strings themselves Excel VBA如何将值视为整数而不是字符串 - Excel VBA how to treat value as a integer rather than as a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM