VBA color the max value inside the parentheses in column A

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good afternoon,

I am having some issues, I need to color the max value inside the parentheses in column A. The code below is what I am working with but cannot get it right. Any help would be appreciated!
VBA Code:
Sub xdoit()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim r As Range
Set r = Range("A1:A" & lastrow)
For Each xcell In r
    If InStr(xcell.Value, "(") > 0 Then
        parts = Split(xcell.Value, "(")
        For i = 0 To UBound(parts)
            v = Val(Replace(parts(i), ")", ""))
            If v > 0 Then
               xcell.Characters(v, 2).Font.ColorIndex = 3
            End If
        Next i
    End If
Next
End Sub



Book1
AB
1ABSNT(9), VAB(8)9
2VAB(2)2
3ABSNT(19), VAB(18)19
4VAB(2)2
5ABSNT(5)5
6ABSNT(47), VAB(7)47
7ABSNT(11), VAB(2)11
8ABSNT(24)24
9VAB(39)39
10ABSNT(6)6
Sheet1
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Give this a try
VBA Code:
Sub DoIt()
    Dim lastrow As Long, i As Long
    Dim j As Long, x As Long, HiNum As Long
    Dim r As Range, xcell As Range, parts As Variant

    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Set r = Range("A1:A" & lastrow)
    
    r.Replace what:=")", replacement:="(("
        
    For Each xcell In r
        parts = Split(xcell.Value, "(")
        For i = 0 To UBound(parts)
            If IsNumeric(parts(i)) And parts(i) > HiNum Then
                HiNum = parts(i)
            End If
        Next i
        
        j = Len(Trim(HiNum))
        xcell.Replace what:="((", replacement:=")"
        x = InStr(xcell.Value, HiNum)
        xcell.Characters(x, j).Font.ColorIndex = 3
        
        HiNum = 0
    Next xcell

End Sub
 
Upvote 0
Ahh!! That does it!! Thank you NoSparks, that helped me a lot!!
 
Upvote 0
Here is another (more compact) macro that you can consider...
VBA Code:
Sub DoIt2()
    Dim MaxVal As String, Cell As Range, Parts As Variant
    For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
      Parts = Split(Cell.Value & "(0", "(")
      MaxVal = CStr(Application.Max(Val(Parts(1)), Val(Parts(2))))
      Cell.Characters(InStr(Cell.Value, MaxVal), Len(MaxVal)).Font.Color = vbRed
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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