在包含其他文本的单元格中查找值的总和

时间:2015-04-02 17:46:58

标签: excel excel-vba excel-formula sum worksheet-function vba

我在单个单元格中有一个包含大量值的Excel工作表,如下所示:

a=5  
b=10  
c=44  
d=33  
...

我想提取每个=字符后的值的总和。

我们怎么能这样做?

3 个答案:

答案 0 :(得分:2)

假设ColumnA中显示的布局,将ColumnA复制到Column,将Text应用于列上的列,=作为分隔符,然后在C5中:

=SUM(C1:C4)  

SO29419251 example

答案 1 :(得分:2)

试试这个小的用户定义函数UDF

Public Function SumInCell(s As String) As Double
    Dim s2 As String, d As Variant
    s2 = s & ","
    ary = Split(s2, "=")

    SumInCell = 0
    For i = LBound(ary) + 1 To UBound(ary)
        s2 = ary(i)
        d = CDbl(Mid(s2, 1, InStr(1, s2, ",") - 1))
        SumInCell = SumInCell + d
    Next i
End Function

例如:

enter image description here

用户定义函数(UDF)非常易于安装和使用:

  1. ALT-F11调出VBE窗口
  2. ALT-I ALT-M打开了一个新模块
  3. 粘贴内容并关闭VBE窗口
  4. 如果保存工作簿,UDF将随之保存。 如果您在2003年之后使用的是Excel版本,则必须保存 该文件为.xlsm而不是.xlsx

    删除UDF:

    1. 按上述方式调出VBE窗口
    2. 清除代码
    3. 关闭VBE窗口
    4. 从Excel使用UDF:

      = SumInCell(A1)

      要了解有关宏的更多信息,请参阅:

      http://www.mvps.org/dmcritchie/excel/getstarted.htm

      http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

      有关UDF的详细信息,请参阅:

      http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

      必须启用宏才能使其生效!

答案 2 :(得分:1)

对于公式答案,您需要在2个单元格中执行此操作,因此您不会达到excel的嵌套限制

如果您的值在A1中,

IN B1 put

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(UPPER(SUBSTITUTE(A1,"=","'")),"A",""),"B",""),"C",""),"D",""),"E",""),"F",""),"G",""),"H",""),"I",""),"J",""),"K",""),"L",""),"M",""),"N",""),"O",""),"P",""),"Q",""),"R",""),"S",""),"T",""),"U",""),"V",""),"W",""),"X",""),"Y",""),"Z","")," ","")&","

在C1放

=SUMPRODUCT(MID(B1,FIND("@",SUBSTITUTE(B1,"'","@",ROW(INDIRECT("1:"&LEN(B1)-LEN(SUBSTITUTE(B1,"'",""))))))+1,FIND("*",SUBSTITUTE(B1,",","*",ROW(INDIRECT("1:"&LEN(B1)-LEN(SUBSTITUTE(B1,",",""))))))-(FIND("@",SUBSTITUTE(B1,"'","@",ROW(INDIRECT("1:"&LEN(B1)-LEN(SUBSTITUTE(B1,"'",""))))))+1))+0)
  

这需要一段时间才能解决:)