• Extract format from cell (2002)

    • This topic has 8 replies, 2 voices, and was last updated 21 years ago.
    Author
    Topic
    #404871

    I have a spreadsheet with about 20,000 rows that came from a database. One of the columns has currency values from different types of currencies (e.g. US $, Euros, etc.) Each of the cells has a custom format that indicates what type of currency it is (e.g. [$EUR] #,##0.00_);([$EUR] #,##0.00) ). What I need to be able to do is extract the currency type from the fomat into another column, so I can determine which currency is appropriate for each cell. Does anyone have an idea about how to do this?

    Thanks,
    Brent

    Viewing 1 reply thread
    Author
    Replies
    • #826876

      This function will get the “currency” within the brackets of the formula. If there are no brackets it assumes the dollar.

      Option Explicit
      Function GetCurrency(rng As Range)
          Dim sForm As String
          Dim iStart As Integer
          Dim iEnd As Integer
          sForm = rng.Cells(1).NumberFormat
          iStart = InStr(sForm, "[")
          iEnd = InStr(sForm, "]")
          If iStart = 0 Then
              GetCurrency = "$"
          Else
              GetCurrency = Mid(sForm, iStart + 1, iEnd - iStart - 2)
          End If
      End Function

      Use in a cell like:
      =getcurrency(A1)

      Steve

      • #826922

        Thanks Steve. That was exactly what I needed. It turned out that I had to make a couple of minor modifications to get it to work (some of the currency names were enclosed in quotes rather than brackets), and the spacing was one off, but other than that it was perfect.

        Thanks,
        Brent

      • #826923

        Thanks Steve. That was exactly what I needed. It turned out that I had to make a couple of minor modifications to get it to work (some of the currency names were enclosed in quotes rather than brackets), and the spacing was one off, but other than that it was perfect.

        Thanks,
        Brent

        • #826928

          Would you mind posting your modifications to the board?

          I am just thinking in case the question comes up from another user later on. When he/she finds this thread, he/she does not have to reinvent your code and its modifications.

          Thanks,
          Steve

          • #826958

            I was wrong about the format initially (it uses quotes instead of square brackets). Here’s an example of the actual format: #,##0.00 “CHF”;-#,##0.00 “CHF”;

            Here’s the modified code:

            Function GetCurrency(rng As Range)
                Dim sForm As String
                Dim iStart As Integer
                Dim iEnd As Integer
                sForm = rng.Cells(1).NumberFormat
                iStart = InStr(sForm, """")
                iEnd = InStr(iStart + 1, sForm, """")
                If iStart = 0 Then
                    GetCurrency = "$"
                Else
                    GetCurrency = Mid(sForm, iStart + 1, iEnd - iStart - 1)
                End If
            End Function
            
          • #826959

            I was wrong about the format initially (it uses quotes instead of square brackets). Here’s an example of the actual format: #,##0.00 “CHF”;-#,##0.00 “CHF”;

            Here’s the modified code:

            Function GetCurrency(rng As Range)
                Dim sForm As String
                Dim iStart As Integer
                Dim iEnd As Integer
                sForm = rng.Cells(1).NumberFormat
                iStart = InStr(sForm, """")
                iEnd = InStr(iStart + 1, sForm, """")
                If iStart = 0 Then
                    GetCurrency = "$"
                Else
                    GetCurrency = Mid(sForm, iStart + 1, iEnd - iStart - 1)
                End If
            End Function
            
        • #826929

          Would you mind posting your modifications to the board?

          I am just thinking in case the question comes up from another user later on. When he/she finds this thread, he/she does not have to reinvent your code and its modifications.

          Thanks,
          Steve

    • #826877

      This function will get the “currency” within the brackets of the formula. If there are no brackets it assumes the dollar.

      Option Explicit
      Function GetCurrency(rng As Range)
          Dim sForm As String
          Dim iStart As Integer
          Dim iEnd As Integer
          sForm = rng.Cells(1).NumberFormat
          iStart = InStr(sForm, "[")
          iEnd = InStr(sForm, "]")
          If iStart = 0 Then
              GetCurrency = "$"
          Else
              GetCurrency = Mid(sForm, iStart + 1, iEnd - iStart - 2)
          End If
      End Function

      Use in a cell like:
      =getcurrency(A1)

      Steve

    Viewing 1 reply thread
    Reply To: Extract format from cell (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: