使用RegExp正则表达式的粗体文本 [英] Bold text using RegExp Regular Expressions
问题描述
我正在尝试使用regEx查找日期并在一个区域中的多个单元格中的文本内将它们加粗,但是看来我没有使用正确的表达式来使用regEx来加粗文本.下面的代码除了粗体功能以外,均在工作.我需要regEx,因为我需要以yyyy-mm-dd的格式将所有日期加粗.我知道下面的regEx表达式不能做到这一点,但是在处理下一部分之前,我要使粗体功能起作用.
I am trying to use regEx to find dates and bold them within text in multiple cells in a range but it appears that I am not using the correct expression for bolding text using regEx. The code below is working other than the bolding function. I need regEx as I need to bold all dates in the format of yyyy-mm-dd. I know that the regEx expression below is not correct to do this but i was going to get the bolding function working before i tackled the next part.
我已经检查了所有其他问题,它们都避免了使用regEx进行粗体显示.
I have checked all the other questions and they all steer away from using regEx for bolding.
Sub Bold_a_date2()
Dim ws As Worksheets
Dim item As Variant
Dim arr As Variant
arr = Worksheets("Formatted").Range("M1:M1000")
Dim regEx As New RegExp
regEx.Global = True
regEx.Pattern = "202[0-9]"
Dim text As Variant
Dim mc As MatchCollection, row As Long
row = 1
For Each text In arr
If regEx.test(text) = True Then
Set mc = regEx.Execute(text)
Selection.Font.Bold = True
Debug.Print text
End If
row = row + 1
Next text
End Sub
推荐答案
如果您的数据是日期值,结果将很奇怪.如果数据为文本格式,则可以执行以下操作.
If your data is a date value, the results will be weird. If the data is in text format, you can do as follows.
Sub Bold_a_date2()
Dim ws As Worksheets
Dim item As Variant
Dim arr As Range
Dim text As Range
Set arr = Worksheets("Formatted").Range("M1:M1000")
'Set arr = Worksheets("Formatted").Range("i1:i1000")
Dim regEx As New RegExp
regEx.Global = True
regEx.Pattern = "202[0-9]"
Dim mc As MatchCollection, row As Long
Dim m As Match
Dim s As Integer, l As Integer
For Each text In arr
If regEx.test(text) = True Then
Set mc = regEx.Execute(text)
For Each m In mc
s = m.FirstIndex
l = m.Length
text.Characters(s + 1, l).Font.Bold = True
Next m
Debug.Print text
End If
Next text
End Sub
字符串与日期值
这篇关于使用RegExp正则表达式的粗体文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!