How to loop columns to find cells formatted as currency, write currency in next col, and reformat

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
Hi,

Let me start with an example;

  • A sheet has two columns, A and B.
  • Col A can be formatted as currency
  • Col B is formatted as text, without data.

There are many rows, and some of the cells will be formatted as currency, others as text. The reason is that data has been appended to the sheet from different sources.

How can I loop thru these columns and

  1. Find cells in col A that are formatted as currency
  2. When/if found the corresponding currency code should be written in to same row, but col B ( $ in col A should be USD in col B)
  3. The cell in col A should be formatted as number with two decimals so the currency symbol does not show
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
That is going to be quite a challenge if not impossible given what you have posted. Being that it appears that the $ symbol is used to represent multiple currencies as shown here, how to know if it should be Tuvalu Dollar or United States Dollar and there are more that use $

1655571940971.png
 
Upvote 0
It might be possible if you have a know set of currencies that you are working with and you can assume that $ is always going to be USD.
 
Upvote 0
It will not always be the same currency I am afraid, it can be at least 20 different currencies.
 
Upvote 0
So what you would need to do is build out your list of currencies and their corresponding values such as $ = USD £ = GBP etc.

Then you can use a for loop to loop through the data and populate the corresponding values.

This is example code that covers 2 currencies, $ which will populate USD, and £ which will populate GBP
You would need to add in all of the other currencies that you want to handle and assuming you dont have any overlap you should be good.

VBA Code:
Sub IdentifyCurrency()

myLastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To myLastRow
checkVal = Range("A" & i).Text
Select Case True
    Case checkVal Like "$*"
        MsgBox "USD"
        Range("B" & i).Value = "USD"
    Case checkVal Like "£*"
        MsgBox "GBP"
        Range("B" & i).Value = "GBP"
End Select

Next i

End Sub

Will produce

1656034101403.png
 
Upvote 0
With data in column A, from cell A1
Currency symbol suppose to 1 digit
VBA Code:
Option Explicit
Sub test()
Dim lr&, cell As Range
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In Range("A1:A" & lr)
    ' case of cell with manual input currency: "$200"
    If Not IsNumeric(cell) And IsNumeric(Right(cell, Len(cell) - 1)) Then
        cell.Offset(, 1).Value = Left(cell, 1)
        cell.Value = Right(cell, Len(cell) - 1)
    ' case of cell with currency format: 200 to displayed as $200
    ElseIf Len(cell.Text) <> Len(cell) Then
        cell.Offset(, 1).Value = Left(cell.Text, 1)
        cell.NumberFormat = "#,###.00"
    End If
Next
End Sub
 
Upvote 0
With data in column A, from cell A1
Currency symbol suppose to 1 digit
VBA Code:
Option Explicit
Sub test()
Dim lr&, cell As Range
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In Range("A1:A" & lr)
    ' case of cell with manual input currency: "$200"
    If Not IsNumeric(cell) And IsNumeric(Right(cell, Len(cell) - 1)) Then
        cell.Offset(, 1).Value = Left(cell, 1)
        cell.Value = Right(cell, Len(cell) - 1)
    ' case of cell with currency format: 200 to displayed as $200
    ElseIf Len(cell.Text) <> Len(cell) Then
        cell.Offset(, 1).Value = Left(cell.Text, 1)
        cell.NumberFormat = "#,###.00"
    End If
Next
End Sub
The code I provided will address points 1 and 2. to loop through all the rows and populate column B based on the currency.

This code that bebo021999 provided will address point 3 which I overlooked, you should run his code after you run mine - Thanks bebo021999
 
Upvote 0
Thanks guys, both macros do in principle work, but with a small hickup. When a sum is formatted as currency, the currency symbol can be after the sum as well as in front of the sum.
If a sum is in USD it will be like this: $1,200,00, but if it is in Euro it will be like this: 1.200,00 €.
 
Upvote 0
Thanks guys, both macros do in principle work, but with a small hickup. When a sum is formatted as currency, the currency symbol can be after the sum as well as in front of the sum.
If a sum is in USD it will be like this: $1,200,00, but if it is in Euro it will be like this: 1.200,00 €.
VBA Code:
Sub IdentifyCurrency()

myLastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To myLastRow
checkVal = Range("A" & i).Text
Select Case True
    Case checkVal Like "*$*"
        Range("B" & i).Value = "USD"
    Case checkVal Like "*£*"
        Range("B" & i).Value = "GBP"
End Select

Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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