VBA to bold all dates within a single cell

Candyman8019

Well-known Member
Joined
Dec 2, 2020
Messages
985
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've been struggling to come up with code to bold all dates within a single cell.

My data looks like this:
1697121892946.png

In this scenario, three dates would be bolded and the rest of the text in the cell would not be.

Any thoughts on how to do this?
 
The below should work for both:
VBA Code:
Sub test()
    Dim rCell As Range
    Dim regEx As Object, regMatches As Object, regMatch As Object
    
    For Each rCell In Range("A1:A3")
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
            .Global = True
            .IgnoreCase = True
            .Pattern = "([A-Za-z]{3} \d{1,2}, \d{4})"
            Set regMatches = .Execute(rCell.Value)
        End With
        For Each regMatch In regMatches
            With regMatch
                rCell.Characters(.FirstIndex + 1, Len(.Value)).Font.Bold = True
            End With
        Next regMatch
    Next rCell
End Sub
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,215,130
Messages
6,123,220
Members
449,091
Latest member
jeremy_bp001

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