1

I received an Excel file containing an extract of sales forecasts. There is no column explicitly defining currency unit. Currency unit is only shown by number formatting (GBP, EUR or CHF). Does anyone know how can I detect the currency format in order to apply appropriate exchange rate for the different currencies?

Example:

Displayed Number Format / Actual Number in Cell

200,000 GBP / 200000

10,000 CHF / 10000

500,000 EUR / 500000

Many thanks, Tym

2 Answers 2

1

Try the following User Defined Function:

Public Function txet(r As Range) As String
    Dim s As String
    s = Replace(r.NumberFormat, Chr(34), "")
    txet = ""
    If Len(s) < 3 Then Exit Function
    s = Right(s, 3)
    txet = s
End Function

enter image description here

User Defined Functions (UDFs) are very easy to install and use:

  1. ALT-F11 brings up the VBE window
  2. ALT-I ALT-M opens a fresh module
  3. paste the stuff in and close the VBE window

If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the UDF:

  1. bring up the VBE window as above
  2. clear the code out
  3. close the VBE window

To use the UDF from Excel:

=txet(A1)

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

1
  • HI Gary's Student, thanks so much for creating and explaining the UDF above. If you have the time I would really like to understand what is occurring on each line, no problem if you don't. This was really easy to implement and produced the output I was seeking. I realised last night that I didn't specify what output I was hoping to achieve, apologies for that. Thanks, Tym
    – Tym Burman
    Commented Jan 29, 2019 at 8:49
0

You can do this in VBA.

Sub CheckFormat()
Dim myCell, j, LastRow
   Worksheets("Sheet1").Activate
   LastRow = Cells(Rows.Count, "A").End(xlUp).Row
   For j = 1 To LastRow
     Set myCell = Cells(j, "A")
     Debug.Print myCell.NumberFormat
   Next j
End Sub

I put your samples in A1, A2, and A3. The above code outputs this:

[$GBP] #,##0.00

[$CHF] #,##0.00

[$EUR] #,##0.00

You can then parse the first parts of that to decide what conversion rate to apply.

2
  • Hi Doug, I created a file CheckFormat_Test.xlsm with the currency-formatted numbers in column A on Sheet1 (starting at A1). I inserted a module into the file and pasted in the CheckFormat() code. Running the macro didn't seem to change the numbers to the format you listed above. Maybe this is significant, the input format actually has 2 decimal places (example 350,000.00 GBP). Many thanks for your support but I am too newbie to diagnose why this is not working, Tym
    – Tym Burman
    Commented Jan 29, 2019 at 8:59
  • My sample code only sends output to the Immediate Window. It was just meant to demonstrate a way to detect the format of a cell, but you'll need to then decide what to do with that knowledge. The answer from Gary's Student does a better job of incorporating the result into the cells next to the formatted values.
    – Doug Deden
    Commented Jan 29, 2019 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .