简体   繁体   中英

Format as different currencies

I have one column as bonus amounts (formatted as number) and another column which has the currency type - could be USD, EUR, GBP, TWD, INR, etc. I need a way to concatenate the two columns and format it as currency so that it can be used in numerical calculations. The currency could be either as symbol or the name USD.

Eg.

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

the "should be" column cannot be text. I need to use this column to display on plans and then use in calculations.

I have tried concatenating as a string and then formatting that column to Currency. But although the format says Currency, it is still a text and any calculations give errors.

You require custom number formatting.

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

A more dynamic approach could be attained through a series of conditional formatting rules.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM