我想用一个额外的if语句来限制选定单元格范围内的所有公式。
使用VBA是否可行?
比方说:单元格A1包含=sum(B1;C1(,但它可以是所选单元格包含的任何公式在应用宏之后,更新的公式将是=if(D100>0;sum(B1;C1(;0(
=如果(D100>0;单元格中的任何公式;0(
您可以通过使用以下代码来实现这一点:
Sub ModifyFormulas()
Dim cl As Range, formulas_cells As Range
Const template = "=if(D100>0,#,0)" '# will be replaced by old formula without '='
On Error Resume Next ' handle an error if there are no formulas in the range
Set formulas_cells = Range("A1:A10").SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not formulas_cells Is Nothing Then
For Each cl In formulas_cells
cl.Formula = Replace(template, "#", Mid(cl.Formula, 2))
Next
End If
End Sub
如果没有vba,我使用以下方法:
-
编辑/替换"="用";xyxy"-这将停止excel重新计算
-
则编辑/替换";sum";用";如果(D100>0;sum">
-
然后编辑/替换"("带有"(;0(";
-
最后替换";xyxy";用"=">
然后一切都正常了,当我不得不更改一些计算时,经常用我的表来做这件事。。。