【问题标题】:Excel formula to sum up column of specific currenciesExcel公式总结特定货币的列
【发布时间】:2018-09-10 02:52:01
【问题描述】:

我在 Excel 中有以下列。

我想要一个可以汇总特定货币单元格的 Excel 公式。单元格采用货币格式。 VBA 用户定义函数也可以,但首选 Excel 公式。

我正在使用 Excel 2016。

编辑:单元格采用货币格式。前面的货币符号前缀不是单元格中的字符串。

【问题讨论】:

  • 我没有投反对票,但我认为这是由于您没有付出任何努力,例如尝试写下公式或 UDF 函数。如果你不提供,你可能会得到更多的反对
  • 感谢您的反馈。支持您的评论
  • 我无法单独使用公式来确定货币前缀。在没有 VBA 的情况下,我能想到的唯一解决方案是将数据拆分为两个单独的列,一个列出货币类型,另一个列出货币值。那么根据货币类型(例如SUMIF)很容易求和,但我不确定这个解决方案是否适合你。

标签: excel vba excel-formula


【解决方案1】:

所以我选择了 UDF 路线——让我知道这是否适合您。如果您需要有关如何启动和运行此功能的帮助,请随时告诉我。

UDF 的语法是 CurrencyVal(您用作“sumif”的范围,一个具有您要求和的格式的单元格)

例如:

如果我有 range(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

【讨论】:

  • 感谢您的回答。赞成。你的公式中的A3是什么?或许第二个参数最好是一个像“JPY”或“USD”这样的字符串来指定货币?
  • 是的,我绝对同意有一个描述性的论点会更有用。但是我选择了当前格式,因为 Cell.NumberFormat 不是简单的“$”或“JPY”——它看起来更像 ($#,###, -$#,###) - - 因此,为了避免错误处理,我选择了我知道的完全匹配。
【解决方案2】:

基于正则表达式的 UDF。这是基于以文本形式显示的货币,即单元格中有美元/欧元等。

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 逐字匹配(区分大小写) '

.*? 匹配任何字符(行终止符除外) *? Quantifier — 匹配零次到无限次,尽可能少,根据需要扩展(惰性)

正向预测(?=~)

断言下面的正则表达式匹配 ~ 匹配字符 ~ 字面意思(区分大小写)


如果单元格中有其他文本,那么您可以尝试:

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

【讨论】:

  • 如果前面的“JPY”或“USD”等货币前缀确实是字符串,您的解决方案将有效。但是,单元格是货币格式。前面的前缀不是单元格中的字符串。
  • 您的问题并不清楚。在这种情况下,您需要使用单元格格式。我会留下答案,以防将来有人需要基于文本的解决方案。
  • 对不起,我的错。我会在问题中添加这些细节。
【解决方案3】:

我对 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多