我在Excel中有以下专栏。
我希望有一个Excel公式可以汇总特定货币的单元格。单元格为货币格式。 VBA用户定义的函数也可以,但是首选参数是Excel公式。
我正在使用Excel 2016。
编辑:单元格为货币格式。前面的货币符号前缀不是单元格中的字符串。
答案 0 :(得分:1)
所以我走了UDF路线-让我知道这是否适合您。如果您需要有关如何启动和运行的帮助,请随时告诉我。
UDF的语法为CurrencyVal(您用作“ sumif”的范围,该单元格具有您要求和的格式)
例如:
如果我有一个范围(A2:A5),其中A2 =欧元,而其他均为美元,那么要获取美元的总和,您可以在任何单元格= CurrencyVal(A2:A5,A3)中输入以下内容。
Option Explicit
Function CurrencyVal(SumCellRange As Range, CurrencySumCell As Range) As Integer
Dim Cell As Variant
Dim SumRange As Integer
For Each Cell In SumCellRange
If Cell.NumberFormat = CurrencySumCell.NumberFormat Then
SumRange = SumRange + Cell
End If
Next Cell
CurrencyVal = SumRange
End Function
答案 1 :(得分:1)
基于正则表达式的UDF。这是基于以文本形式显示的货币,即单元格中有USD / EUR等。
Option Explicit
Public Function GetCurrencySum(ByVal rng As Range, ByVal aCurrency As String) As Variant
Dim inputString As String, arr()
If rng.Columns.Count > 1 Then
GetCurrencySum = CVErr(xlErrNA)
Exit Function
End If
Select Case rng.Count
Case 1
ReDim arr(0): arr(0) = rng.Value
Case Else
arr = rng.Value
End Select
inputString = Join(Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, 1)), "~") & "~"
Dim matches As Object, match As Object
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.Pattern = "[+-]?" & aCurrency & ".*?(?=~)"
On Error GoTo errhand:
If .TEST(inputString) Then
Set matches = .Execute(inputString)
For Each match In matches
GetCurrencySum = GetCurrencySum + CDbl(Replace$(match, aCurrency, vbNullString))
Next
Exit Function
End If
GetCurrencySum = 0
Exit Function
End With
errhand:
GetCurrencySum = CVErr(xlErrNA)
End Function
工作表中:
正则表达式:
here试试。
[+-]?JPY.*?(?=~)
/
gm
匹配[+-]?
下面的列表中存在的单个字符
?
量词-匹配0到1次,尽可能多地匹配,并根据需要返回(贪婪)
+-
与列表+-
中的单个字符匹配(区分大小写)
JPY
从字面上匹配字符JPY
(区分大小写)'
.*?
匹配任何字符(行终止符除外)
*?
量词-匹配零到无限次,尽可能少地次数,根据需要扩展(延迟)
正面提前(?=~)
声明以下正则表达式匹配
~
从字面上匹配字符~
(区分大小写)
如果单元格中还有其他文本,则可以尝试:
Public Function GetCurrencySum(ByVal rng As Range, ByVal aCurrency As String) As Variant
Dim inputString As String, arr()
If rng.Columns.Count > 1 Then
GetCurrencySum = CVErr(xlErrNA)
Exit Function
End If
Select Case rng.Count
Case 1
ReDim arr(0): arr(0) = rng.Value
Case Else
arr = rng.Value
End Select
inputString = Join(Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, 1)), "~") & "~"
Dim matches As Object, match As Object
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.Pattern = "[\-\+]?" & aCurrency & "\s[\d,.]+"
On Error GoTo errhand:
If .test(inputString) Then
Set matches = .Execute(inputString)
For Each match In matches
GetCurrencySum = GetCurrencySum + CDbl(Replace$(Replace$(match, aCurrency, vbNullString), "~", vbNullString))
Next
Exit Function
End If
GetCurrencySum = 0
Exit Function
End With
errhand:
GetCurrencySum = CVErr(xlErrNA)
End Function
here试试。
答案 2 :(得分:1)
我对Dylan的答案进行了一些修改,以进行一些自定义,以适应自己的喜好。我将此答案发布到我自己的问题上,以备将来参考。
假设存在一个范围(A2:A5),其中A2 =欧元,而所有其他货币均为美元,则要获取美元的总和,您可以在任意单元格=GetCurrencySum(A2:A5, "[$USD] #,##0.00")
中输入以下内容。
Function GetCurrencySum(SumCellRange As Range, CurrencyFormat As String) As Single
On Error GoTo errorhd
Dim Cell As Variant
Dim SumRange As Single
SumRange = 0
For Each Cell In SumCellRange
If Cell.NumberFormat = CurrencyFormat Then
SumRange = SumRange + Cell
End If
Next Cell
GetCurrencySum = SumRange
Exit Function
errorhd:
MsgBox Err.Source & "-->" & Err.Description, , "CurrencyVal"
End Function