Sum with substitute, ignore and a dynamic range

五迷三道 提交于 2021-01-28 19:23:55

问题


I came pretty close to solving my problem with this answer given by "barry houdini" But I run into problems incorporating it to my needs.

 =SUMPRODUCT(VALUE(0&SUBSTITUTE(A1:A8,"*","")))

Edit #3 - @Rory came with a very good answer to solving my problem. But what I hadn't thought about was if you enter a non-numerical value inside a cell, then the formula returns an error. So I would really like to tweak the following formula to ignore the letter "x", or non-numerical values (if that's easier):

-

ActiveCell.FormulaR1C1 = "=SUMPRODUCT(VALUE(0&SUBSTITUTE(INDIRECT(""R10C:R[-1]C"",FALSE),""s"","""")))" Dim Cell As Range For Each Cell In Range("all_cells") Cell.Formula = Range("one_cell").Formula Next

What I'm now trying to do, is write a VBA script to summarize a dynamic range, AND include values that have an "S" behind them BUT not fail or return an error on any other letters. Most importantly the letter "x".

After that first formula is set, I want to autofill that formula to the right ; stopping on a known column.

End edit

For the sake of history and other google searches, here's the original code I used before having the "s" and "x" problem.

ActiveCell.FormulaR1C1 = _
    "=SUM(INDIRECT(ADDRESS(10,COLUMN())&"":""&ADDRESS(ROW()-1,COLUMN())))"

What this does is basically summarize from a set row (row#10) and stop the summary on the row just before the cell with this formula.

Example of what I want to achieve: "3+4S+2 + x = 9" ; instead of "= 5" or "##"


回答1:


I finally solved my problem! I realized nesting the substitute formulas could be a nifty solution, and after several errors I realized that the range only needed to be mentioned once, and voila!

ActiveCell.FormulaR1C1 = "=SUMPRODUCT(VALUE(0&SUBSTITUTE(0&SUBSTITUTE(INDIRECT(""R10C:R[-1]C"",FALSE),""s"",""""),""x"","""")))"



来源:https://stackoverflow.com/questions/31657406/sum-with-substitute-ignore-and-a-dynamic-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!