Trying to change cells to GBP currency format using VBA

itm

New Member
Joined
Mar 20, 2010
Messages
9
I am trying to write some VBA (in Excel 2016) to look for USD currency values in a range of cells and change the cell format to GBP. The code below spots the USD cells (i.e. the If statement is reached, but the cells remain formatted as "$n.nn". Could anyone explain what I'm doing wrong?
VBA Code:
Sub ChangeCurrency()
Dim cl As Range


For Each cl In Intersect(ActiveSheet.Range("C24:I26"), ActiveSheet.UsedRange)
        If InStr(1, cl.Text, "$") > 0 Then
            cl.Style = "Currency"
            cl.NumberFormat = "[$£-809]#,##0.00"
        End If


Next cl
 
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
see if this update to your code does what you want

VBA Code:
Sub ChangeCurrency()
    Dim cl As Range
    
    For Each cl In ActiveSheet.Range("C24:I26")
            If InStr(1, cl.Text, "$") > 0 Then
                cl.Value = CCur(cl.Value)
                cl.Style = "Currency"
                cl.NumberFormat = "[$£-809]#,##0.00"
            End If
    Next cl
 
End Sub

Dave
 
Upvote 0
FWIW, I tested your code as-is and it works perfectly.
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Sub ChangeCurrency()
    Dim cl As Range
   
    For Each cl In ActiveSheet.Range("C24:I26")
            If InStr(1, cl.Text, "$") > 0 Then
                cl.Value = CCur(cl.Value)
                cl.Style = "Currency"
                cl.NumberFormat = "[$£-809]#,##0.00"
            End If
    Next cl
 
End Sub

Dave
I get a Type Mismatch on this line:
cl.Value = CCur(cl.Value)

The cell in question contains a value of $1.39 and has a format of Currency
???
 
Upvote 0
VBA Code:
    With Range("C4:I26")
        .NumberFormat = "£#,##0.00"
    End With
 
Upvote 0
VBA Code:
    With Range("C4:I26")
        .NumberFormat = "£#,##0.00"
    End With
Not all of the cells in that range contain currency values - hence the original loop structure and If statement.

If I try this I get the same result (i.e. none of the cells are altered):
VBA Code:
With Range("C4:I26")
        If InStr(1, .Text, "$") > 0 Then
            .NumberFormat = "£#,##0.00"
        End If
    End With
 
Upvote 0
Error suggests some of your cells contain strings

Try this update

VBA Code:
Sub ChangeCurrency()
    Dim cl As Range
    
    For Each cl In ActiveSheet.Range("C24:I26")
        If InStr(1, cl.Text, "$") > 0 Then
            With cl
                If IsNumeric(.Value) Then .Value = CCur(cl.Value) Else .Value = CCur(Mid(.Value, 2))
                .Style = "Currency"
                .NumberFormat = "[$£-809]#,##0.00"
            End With
        End If
    Next cl
 
End Sub

Dave
 
Upvote 0
Not all of the cells in that range contain currency values - hence the original loop structure and If statement.

If I try this I get the same result (i.e. none of the cells are altered):
VBA Code:
With Range("C4:I26")
        If InStr(1, .Text, "$") > 0 Then
            .NumberFormat = "£#,##0.00"
        End If
    End With

You can't use a With/End With like that. That would never work.

Try the following:

VBA Code:
    Dim cl As Range
    
    For Each cl In ActiveSheet.Range("C24:I26")
        If InStr(1, cl.Text, "$") > 0 Then
            cl.NumberFormat = "£#,##0.00"
        End If
    Next cl
 
Upvote 0
Error suggests some of your cells contain strings

Try this update

VBA Code:
Sub ChangeCurrency()
    Dim cl As Range
   
    For Each cl In ActiveSheet.Range("C24:I26")
        If InStr(1, cl.Text, "$") > 0 Then
            With cl
                If IsNumeric(.Value) Then .Value = CCur(cl.Value) Else .Value = CCur(Mid(.Value, 2))
                .Style = "Currency"
                .NumberFormat = "[$£-809]#,##0.00"
            End With
        End If
    Next cl
 
End Sub

Dave
That's it - perfect!
Many thanks
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,746
Members
449,050
Latest member
excelknuckles

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