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?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi all,

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

My data looks like this:
View attachment 100220
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?
Would you ever have another date in the body of the notes? Example: Sep 18, 2023 - Sent message to xxxx xxxx on Sep 15, 2023?
 
Upvote 0
I was working on the below but I have to go home now. The below will only work if the date is at the begining of a new row within the cell. It may help with finding the solution:
VBA Code:
Sub test()
    Dim rng As Range
    Dim rCell As Range
    Dim tVar As Variant, x As Long
   
    Set rng = Range("A1:A3")
   
    For Each rCell In rng
        tVar = Split(rCell, Chr(10))
        For x = 0 To UBound(tVar)
            If IsNumeric(Application.Match(Left(tVar(x), 3), Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), 0)) Then
                rCell.Characters(InStr(rCell, Left(tVar(x), 12)), 12).Font.Bold = True
            End If
        Next x
    Next rCell
End Sub

Book1
A
1Sep 18, 2023 - blah blah. Sep 21, 2023 - jkjlqwdjlqwjjo;jk;. Sep 22, 2023 - jjjajjaja - shhshshh
2Sep 18, 2023 - blah blah. Sep 21, 2023 - jkjlqwdjlqwjjo;jk;. hdhdhhdhdh Sep 22, 2023 - jjjajjaja - shhshshh
3Sep 18, 2023 - blah blah. Sep 21, 2023 - jkjlqwdjlqwjjo;jk;. Sep 22, 2023 - jjjajjaja - shhshshh
Sheet1
 
Upvote 0
Okay, one slight issue...if there is more than one comment on the same date, it only bolds the first instance of it.

1697125502724.png


Thoughts?
 
Upvote 0
Give the below a try instead:
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{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
Solution
Just an FYI, I had to use both solutions together. The second part didn't bold any dates with single digit days...so putting both pieces together covered all of the bases.
 
Upvote 0

Forum statistics

Threads
1,215,151
Messages
6,123,321
Members
449,094
Latest member
Chestertim

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