0
点赞
收藏
分享

微信扫一扫

excel中使用vba取出单元格中数字并求和

在excel中alt+F11新建

Function SumDigits(str As String, Optional avg As Boolean) As Double

With CreateObject("vbscript.regexp")

   .Global = True

   .Pattern = "-?\d+(?:\.\d+)?"

   If .Test(str) Then

       Set Matches = .Execute(str)

       For Each Match In Matches

           SumDigits = SumDigits + Match

       Next

       If avg Then SumDigits = SumDigits / Matches.Count

   End If

End With

End Function


然后在单元格中使用新建的vba函数。第2个参数是是否取平均数。

=SumDigits(C2,FALSE)

举报

相关推荐

0 条评论