VBA to address multiple currecy

gdgonzal

New Member
Joined
Jul 1, 2008
Messages
1
Hello,

I have a macro below that separates currency from a custom format. It works fine but as you can see it only addresses CAN and I need to be able to add additional currency like the ones I have listed below. How can I modify this vba to accomplish this?

Need to add:
"#,##0" ZAR";-#,##0" ZAR";#,##0;@"
"#,##0" INR";-#,##0" INR";#,##0;@"
"" £"#,##0;" £"-#,##0;#,##0;@" INR";#,##0;@" (Note this is a prefix symbol while the others listed about are suffixes)



Sub SeparateCurrency()Dim cell As RangeDim ConvertRange As RangeDim LastRow As LongDim FormatString As String LastRow = Range("A65536").End(xlUp).Row Set ConvertRange = ActiveSheet.Range("A1:A" & LastRow) For Each cell In ConvertRange FormatString = cell.NumberFormat cell.Select If cell.NumberFormat = "#,##0"" CAD"";-#,##0"" CAD"";#,##0;@" Then cell.Offset(0, 1).Value = "CAD" cell.Offset(0, 2).Value = cell.Value End If Next End Sub
</PRE>
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the board! A couple of options might be to repeat the "if...end if" statement that you already have, and just create a new one for each format type, or you could use a Case Select statement as below.

Code:
Sub SeparateCurrency()
    Dim rngCel As Range
    Dim ConvertRange As Range
    Dim LastRow As Long
    Dim FormatString As String
    LastRow = Range("A65536").End(xlUp).Row
    Set ConvertRange = ActiveSheet.Range("A1:A" & LastRow)
    For Each rngCel In ConvertRange
        FormatString = rngCel.NumberFormat
        Select Case FormatString
            Case "#,##0"" CAD"";-#,##0"" CAD"";#,##0;@"
                rngCel.Offset(0, 1).Value = "CAD"
                rngCel.Offset(0, 2).Value = rngCel.Value
            Case "#,##0"" ZAR"";-#,##0"" ZAR"";#,##0;@"
                rngCel.Offset(0, 1).Value = "ZAR"
                rngCel.Offset(0, 2).Value = rngCel.Value
            Case "#,##0"" INR"";-#,##0"" INR"";#,##0;@"
                rngCel.Offset(0, 1).Value = "INR"
                rngCel.Offset(0, 2).Value = rngCel.Value
            Case """ £""#,##0;"" £""-#,##0;#,##0;@"" INR"";#,##0;@"
                rngCel.Offset(0, 1).Value = "£"
                rngCel.Offset(0, 2).Value = rngCel.Value
            Case Else
                rngCel.Offset(0, 1).Value = "Currency not specified"
                rngCel.Offset(0, 2).Value = rngCel.Value
        End Select
    Next
End Sub

Note that you can make your code easier to read by using code tags... Put this *after* your code:
[/code]
...and this before it:
Code:
Also note that the variable name "cell" is not used as it could cause you to get scolded by folks on this forum who know more than me - and it could potentially cause weird issues with the VBA too!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top