这里有一些非常有用的代码,我认为我可能会尝试学习并适应我的目的,我试图在电子表格上匹配一个代表一年的4位数字符串,即 . “ 2016 was a good year " would yield " 2016 ” .
我在那里发布的问题中使用了一些略微改变的代码,它设法识别出一个字符串包含一年,但是我不确定如何从其余的单元格内容中分离和提取字符串,即 . 在相邻的单元格中自己获得 2016 ,我应该做出什么改变?
Private Sub splitUpRegexPattern()
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("D2:D244")
For Each c In Myrange
strPattern = "([0-9]{4})" 'looks for (4 consecutive numbers)
If strPattern <> "" Then
strInput = c.Value
strReplace = "$1"
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
c.Offset(0, 5) = regEx.Replace(strInput, "$1") 'puts the string in an adjacent cell
Else
c.Offset(0, 5) = "(Not matched)"
End If
End If
Next
End Sub