Results 1 to 13 of 13

Macro to convert specific text and format specific text within a string

Threaded View

  1. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to convert specific text and format specific text within a string

    Different method
    Option Explicit
    
    Sub test()
        Dim r As Range, temp, num
        Const div As Double = 22.046
        With CreateObject("VBScript.RegExp")
            For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
                If r.Value Like "*USD/MT" Then
                    r.Value = Replace(r.Value, "USD/MT", "USC/LB")
                    .Pattern = "(\+|\-)(\d+(\.\d+))"
                    If .test(r.Value) Then
                        num = Format$(Round(CDbl(.Execute(r.Value)(0).submatches(1)) / div, 2), "#.0000")
                        r.Value = .Replace(r.Value, .Execute(r.Value)(0).submatches(0) & num)
                        r.Characters(1, 2).Font.Bold = True
                        r.Characters(.Execute(r.Value)(0).firstindex + 2, _
                        .Execute(r.Value)(0).Length - 1).Font.Color = vbRed
                    End If
                    .Pattern = "USC/LB"
                    r.Characters(.Execute(r.Value)(0).firstindex + 1, .Execute(r.Value)(0).Length).Font.Bold = True
                End If
            Next
        End With
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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