2

I'm modifying an existing application written in vb.net. There is an export option which exports information to an excel sheet. Before exporting the cell format is defined as follows.

.Cells(cRow, 17).NumberFormat = "$#,##0.00_);($#,##0.00)"

this works fine. But according to the new requirement, currency symbol is dynamic. Therefore once I set the currency (eg : Malaysian riggit -MYR),

.Cells(cRow, 17).NumberFormat = "MYR#,##0.00_);(MYR#,##0.00)"

this gives an error "Unable to set the NumberFormat property of the Range class". there can be many currencies and I tried setting the currency using a variable.

Dim strCurrency As String = ""
strCurrency = "SGD"
.Cells(cRow, 17).NumberFormat = ""+strCurrency +"#,##0.00_);("+strCurrency +"#,##0.00)"

All are giving me the same error. Someone please let me know how to set the custom currency symbol. thanks

3 Answers 3

2

If I'm not mistaken any characters in an EXCEL numeric format must be enclosed in quotation marks so:

Dim strCurrency As String = ""
strCurrency = "SGD"
.Cells(cRow, 17).NumberFormat = """"+strCurrency +"""#,##0.00_);("""+strCurrency +"""#,##0.00)"
Sign up to request clarification or add additional context in comments.

1 Comment

You are correct .it's not a mistake. Your code works nicely .thanks
2

Use this

.Cells(cRow, 17).NumberFormat = strCurrency & "#,##0.00_);(" & strCurrency & "#,##0.00)"

There is no need to use so many quotes as shown in the answer that is accepted.

Explanation

Your string

"MYR#,##0.00_);(MYR#,##0.00)"

can also be written as

"MYR" & "#,##0.00_);(" & "MYR" & "#,##0.00)"

Now simply replace "MYR" with your variable

Comments

0

Enclose the currency symbol inside quotation :

Dim strCurrency = Chr(34) & "SGD" & Chr(34)
.Cells(cRow, 17).NumberFormat = String.Format("{0}#,##0.00_);({0}#,##0.00)", strCurrency)

The Chr(34) = "

1 Comment

Please provide an explanation as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.