gpt4 book ai didi

excel - 使用 RegExp 正则表达式的粗体文本

转载 作者:行者123 更新时间:2023-12-04 08:11:32 24 4
gpt4 key购买 nike

我正在尝试使用 regEx 来查找日期并在一个范围内的多个单元格的文本中加粗它们,但似乎我没有使用正确的表达式来使用 regEx 加粗文本。下面的代码在除粗体功能之外工作。我需要正则表达式,因为我需要以 yyyy-mm-dd 格式加粗所有日期。我知道下面的 regEx 表达式不正确,但我要在处理下一部分之前让粗体功能正常工作。
我检查了所有其他问题,它们都避免使用正则表达式进行粗体显示。

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
enter image description here

最佳答案

如果您的数据是日期值,结果会很奇怪。如果数据是文本格式,您可以执行以下操作。

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
字符串与日期值
enter image description here
多案例
enter image description here

关于excel - 使用 RegExp 正则表达式的粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65929181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com