Bolding only numbers, % and () in a string, and not texts in between

seekyourway

New Member
Joined
Nov 1, 2011
Messages
21
Hi,

I initially got some help with some assistance, but it's not exactly doing intended. I just wanted to bold numbers, % and () in a string, not the texts in between.

sample file:

fjdakgjdkafaj 532253. 50%
dgjdalg 32523432
gdfjadgad (50%) dajgkdalfdksgda. 235234 (53%).adgjdakfdg
adgdfad (50%). Adgjkdfadg 2000(50%).gdafdgd

When i run the code, i got:

fjdakgjdkafaj 532253. 50%
dgjdalg 32523432
gdfjadgad (50%) dajgkdalfdksgda. 235234 (53%).adgjdakfdg
adgdfad (50%). Adgjkdfadg 2000(50%).gdafdgd

But I don't want to bold the texts in between numbers, % and ().

Code I have:

Sub Bolder()
Dim rng As Range, r As Range, strR As String
Dim i As Long, j As Long, k As Long
Set rng = Intersect(Columns(1), ActiveSheet.UsedRange)
For Each r In rng
strR = r.Text
j = 0: k = 0
For i = 1 To Len(strR)
Select Case Asc(Mid(strR, i, 1))
Case Asc(0) To Asc(9), Asc("("), Asc(")"), Asc("%")
If j = 0 Then j = i
k = i
End Select
Next i
r.Characters(j, k - j + 1).Font.Bold = True
Next r
End Sub


Your assistance is greatly appreciated.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How about
VBA Code:
Sub seekyourway()
   Dim Cl As Range
   Dim i As Long
   
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      For i = 1 To Len(Cl.Value)
         Select Case Asc(Mid(Cl, i, 1))
            Case 48 To 57, 40, 41, 37
               Cl.Characters(i, 1).Font.Bold = True
         End Select
      Next i
   Next Cl
End Sub
 
Upvote 0
I think you have to explicitly turn off the font attributes after you set them.

Try:
VBA Code:
Sub Bolder()
Dim rng As Range
Dim r As Range
Dim strR As String
Dim i As Long
Dim j As Long
Dim k As Long
Set rng = Intersect(Columns(1), ActiveSheet.UsedRange)

For Each r In rng
strR = r.Text
'j = 0: k = 0
For i = 1 To Len(strR)
Select Case Asc(Mid(strR, i, 1))
Case Asc(0) To Asc(9), Asc("("), Asc(")"), Asc("%")
'If j = 0 Then j = i
'k = i
r.Characters(i).Font.Bold = True
Case Else
r.Characters(i).Font.Bold = False
End Select
Next i
'r.Characters(j, k - j + 1).Font.Bold = True
Next r
End Sub

Setting the font character by character will (probably) be a little slower, but whether that matters depends on the size of the file
 
Upvote 0
A little testing indicates that Fluff's solution is significantly faster (~double).
 
Upvote 0
Using
VBA Code:
r.Characters(i).Font.Bold = True
will make the font bold from "i" to the end of the string, rather than just that character.
 
Upvote 0
Using
VBA Code:
r.Characters(i).Font.Bold = True
will make the font bold from "i" to the end of the string, rather than just that character.
Yes - something I once knew, but had forgotten
 
Upvote 0
How about
VBA Code:
Sub seekyourway()
   Dim Cl As Range
   Dim i As Long
  
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      For i = 1 To Len(Cl.Value)
         Select Case Asc(Mid(Cl, i, 1))
            Case 48 To 57, 40, 41, 37
               Cl.Characters(i, 1).Font.Bold = True
         End Select
      Next i
   Next Cl
End Sub
thank you works perfectly!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
How about
Rich (BB code):
Sub seekyourway()
   Dim Cl As Range
   Dim i As Long
  
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      For i = 1 To Len(Cl.Value)
         Select Case Asc(Mid(Cl, i, 1))
            Case 48 To 57, 40, 41, 37
               Cl.Characters(i, 1).Font.Bold = True
         End Select
      Next i
   Next Cl
End Sub
This is untested but I am pretty sure you can replace what I highlighted in red above with the following single line of code
VBA Code:
Cl.Characters(i, 1).Font.Bold = Mid(Cl, i, 1) Like "[0-9%()]"
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,800
Members
449,189
Latest member
kristinh

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