+ Reply to Thread
Results 1 to 4 of 4

Amend all currency/number formats as per one cell currency format

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    182

    Amend all currency/number formats as per one cell currency format

    Hi

    I have a macro which change currency of selected cells in a worksheet based on the currency format of a cell called "TSI"

    Private Sub Worksheet_Change(ByVal Target As Range)
     
    Dim Rg As Range
    
    
    
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect([TSI], Target) Is Nothing Then Exit Sub
    Set Rg = Range("C22:D29,E22:F29,C36:F44,C48:F54,C61:F69,C75:F81,C83:D87,F87,D89,")
    
    
    Select Case [TSI].NumberFormat
    
       Case "#,##0.00 [$USD]": Rg.NumberFormat = "#,##0.00 [$USD]"
       Case "#,##0.00 [$EUR]": Rg.NumberFormat = "#,##0.00 [$EUR]"
    End Select
    
    End Sub
    This works fine, but i would like to do it easier to change all number /currency format in all the worksheets (excluding percentage and dates format of course) as per cell "TSI" currency format, can someone help?

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Amend all currency/number formats as per one cell currency format

    Assuming TSI is in Sheet1, try this:
    Public Sub ChangeCurrencies()
    Dim wks As Worksheet
    Dim cel As Range, rngNumbers As Range
    Dim strFind As String, strReplace As String
    
    Const conUSD = "#,##0.00 [$USD]"
    Const conEUR = "#,##0.00 [$EUR]"
        
        strReplace = Sheet1.Range("TSI").NumberFormat
        If strReplace = conUSD Then
            strFind = conEUR
        ElseIf strReplace = conEUR Then
            strFind = conUSD
        Else
            MsgBox ("TSI currency format does not match.")
        End If
        For Each wks In ThisWorkbook.Worksheets
            wks.Activate
            Set rngNumbers = wks.Cells.SpecialCells(xlCellTypeConstants, 1)
            If Not rngNumbers Is Nothing Then
                For Each cel In rngNumbers.Cells
                    If cel.NumberFormat = strFind Then cel.NumberFormat = strReplace
                Next cel
            End If
        Next wks
        
    End Sub
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    182

    Re: Amend all currency/number formats as per one cell currency format

    thank you there was some inversion in your code that i corrected, however i would like to run it in the active worsheet meaning

    My workbook has 3 worksheet, i kepp only the relevant one deleting the useless ones for my case,
    Each worksheet has a cell named "TSI" on which should be based all the currency formated cells in the worsheet

    if in the active worksheet, upon changed in TSI value/currency format(EUR or else), then all the numbered formatted cells in the active worsheet sshould have same format than "TSI"
    it should apply only on yhe number formatted cells not on the other ones fromatted for example as date or percentage

    i guess i should put the code under each worksheet since some will be deleted

    could you help me

    Private Sub Worksheet_Change(ByVal Target As Range)
     
    Dim cell As Range, rngNumbers As Range
    Dim strFind As String, strReplace As String
    
    Const conUSD = "[$USD] #,##0.00"
    Const conEUR = "[$EUR] #,##0.00"
        
        'each time "TSI" is modified (number and/or currency)
        strReplace = Range("TSI").NumberFormat
    
            'find and apply macro on each cells formatted as "number" in the worksheet
              Set rngNumbers = UsedRange.Cells.SpecialCells(xlCellTypeConstants, 1)
            If Not rngNumbers Is Nothing Then
                For Each cell In rngNumbers.Cells
                    If cell.NumberFormat = strFind Then cel.NumberFormat = strReplace
                Next cell
            End If
       
        
        
    End Sub

  4. #4
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    182

    Re: Amend all currency/number formats as per one cell currency format

    I managed to do it, each time "TSI" value change, below macro looks for all the cells numbered formatted (excluding percentage) change the format of the cell as per TSI format

    Private Sub Worksheet_Change(ByVal Target As Range)
     
    Dim cel As Range, rngNumbers As Range
    Dim strFind As String, strReplace As String
    
    Const conUSD = "#,##0.00 [$USD]"
    Const conEUR = "#,##0.00 [$EUR]"
        
        
        If Not Intersect(Target, Target.Worksheet.Range("TSI")) Is Nothing Then
        strReplace = Range("TSI").NumberFormat
           
            Set rngNumbers = Cells.SpecialCells(xlCellTypeConstants, 1)
            If Not rngNumbers Is Nothing Then
                For Each cel In ActiveSheet.UsedRange
                    If IsNumeric(cel) = True And cel.NumberFormat <> "0.0000%" Then cel.NumberFormat = strReplace
                Next cel
            End If
            
         End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Amend currency of a range if currency format of a cell change
    By mariec_06 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-07-2022, 02:56 PM
  2. [SOLVED] Macro to convert Number Stored as Text and Format in appropriate Currency Formats
    By sameer79 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-28-2020, 03:07 PM
  3. Replies: 5
    Last Post: 06-22-2020, 10:28 AM
  4. Replies: 2
    Last Post: 12-09-2016, 07:18 AM
  5. [SOLVED] Number Formats for Large Currency Requested
    By dbsbender in forum Excel General
    Replies: 5
    Last Post: 10-12-2014, 07:06 PM
  6. Excel 2003 formula: keep currency format when putting text and number in same cell
    By cdstelco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2011, 05:32 PM
  7. Currency Formatting-range of number as currency
    By kmurray24 in forum Excel General
    Replies: 1
    Last Post: 01-09-2008, 09:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1