【问题标题】:Format as different currencies格式化为不同的货币
【发布时间】:2018-02-16 03:42:04
【问题描述】:

我有一列作为奖金金额(格式为数字),另一列具有货币类型 - 可以是美元、欧元、英镑、新台币、印度卢比等。 我需要一种方法来连接两列并将其格式化为货币,以便它可以用于数值计算。货币可以是符号或名称 USD。

例如。

Amount   Currency    Should be
5000      USD         USD 5000 OR $ 5000
6000      GBP         GBP 6000 OR (Symbol) 6000

“应该是”列不能是文本。我需要使用此列在计划中显示,然后在计算中使用。

我尝试连接为字符串,然后将该列格式化为货币。但尽管格式显示货币,但它仍然是文本,任何计算都会出错。

【问题讨论】:

  • 显然USD 5000 不是数值(它是一个以USD 开头的字符串),$ 5000 也不是(同样,一个以$ 开头的字符串)。在进行连接之前使用Amount 列进行计算,然后根据需要显示结果。

标签: excel format currency


【解决方案1】:

您需要自定义数字格式。

dim rw as long
with worksheets("sheet1")
    for rw = 2 to .cells(.rows.count, "A").end(xlup).row
        .cells(rw, "C") = .cells(rw, "A").value2
        select case lcase(.cells(rw, "B").value2)
            case "usd"
                .cells(rw, "C").numberformat = "\U\S\D 0"
            case "eur"
                .cells(rw, "C").numberformat = "\E\U\R 0"
            case "gpd"
                .cells(rw, "C").numberformat = "\G\P\D 0"
            case "twd"
                .cells(rw, "C").numberformat = "\T\W\D 0"
            case "inr"
                .cells(rw, "C").numberformat = "\I\N\R 0"
        end select
    next rw
end with

可以通过一系列条件格式规则获得更动态的方法。

with worksheets("sheet1").range(c:c)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B1=""USD"""
    .FormatConditions(.FormatConditions.count).numberformat = "\U\S\D 0"
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B1=""EUR"""
    .FormatConditions(.FormatConditions.count).numberformat = "\E\U\R 0"
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B1=""GBP"""
    .FormatConditions(.FormatConditions.count).numberformat = "\G\B\P 0"
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B1=""TWD"""
    .FormatConditions(.FormatConditions.count).numberformat = "\T\W\D 0
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B1=""INR"""
    .FormatConditions(.FormatConditions.count).numberformat = "\I\N\R 0"
end with

【讨论】:

    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2016-04-25
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    相关资源
    最近更新 更多