Color last word in a range of cells

IoanZ

Board Regular
Joined
Mar 2, 2018
Messages
51
Hello
I want to know which is the simple vba code to color last word from a range of cells.[TABLE="class: text_table, width: 500"]
<tbody style="margin: 0px; padding: 0px; border: 0px; font-family: inherit; vertical-align: baseline;">[TR]
[TD]1[/TD]
[TD]John cell phone //991612[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Maria cell phone //842246[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Bill cell phone //892246[/TD]
[/TR]
</tbody>[/TABLE]

i Want to color only the numbers (last word in fact)


I have tried


Sub Color_Part_of_Cell()
'Print ActiveCell.Characters.Count
With Range("D3:D11").Characters(, 8).Font
.Color = RGB(36, 182, 36)
End With


End Sub

But this code colors the first letters


Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG19Jun12
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Sp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range("B1", Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] InStr(Dn.Value, " //") > 0 [COLOR="Navy"]Then[/COLOR]
        Sp = Split(Dn.Value, " //")
        Dn.Characters(InStr(Dn.Value, Sp(UBound(Sp))), Len(Sp(UBound(Sp)))).Font.Color = RGB(36, 182, 36)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
You should loop through all the cells in the range looking for the first space from the right, using InStrRrv function

Try with this code

Code:
Sub Color_Part_of_Cell()
Dim r As Range
    For Each r In Range("B1:B3")
        r.Characters(InStrRev(r.Value, " ") + 1, 8).Font.Color = vbRed
    Next r
End Sub

Best regards,
Jovan
 
Upvote 0
.. or even this?
Code:
Sub Test()
  Dim c As Range
  For Each c In Range("B1", Range("B" & Rows.Count).End(xlUp))
    c.Characters(InStr(1, c.Value & "//", "//") + 2, 999).Font.Color = RGB(36, 182, 36)
  Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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