Sub Test()
Dim strTest As String
Dim strTemp As String
strTest = Sheet1.Cells(1, 1).Value
MsgBox RE6(strTest)
Sheet1.Cells(2, 1).Value = RE6(strTest)
End Sub
Function RE6(strData As String)
Dim RE As Object 'REMatches As Object
Dim P As String, A As String
Dim Q As String, B As String
Dim R As String, C As String
Dim S As String, D As String
Dim T As String, E As String
Dim U As String, F As String
Dim V As String, G As String
Dim W As String, H As String
Dim N As Integer
Set RE = CreateObject("vbscript.regexp")
P = "(?:^|\b)He"
A = "She"
Q = "(?:^|\b)he"
B = "she"
R = "(?:^|\b)Him"
C = "Her"
S = "(?:^|\b)him"
D = "her"
T = "(?:^|\b)Himself"
E = "Herself"
U = "(?:^|\b)himself"
F = "herself"
V = "(?:^|\b)His"
G = "Her"
W = "(?:^|\b)his"
H = "her"
'This section replaces "He" with"She"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = P
End With
RE6 = RE.Replace(strData, A)
'This section replaces "he" with "she"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = Q
End With
RE6 = RE.Replace(strData, B)
'
'This section replaces "Him" with "Her"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = R
End With
RE6 = RE.Replace(strData, C)
'This section replaces "him" with "her"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = S
End With
RE6 = RE.Replace(strData, D)
'This section replaces "Himself" with "Herself"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = T
End With
RE6 = RE.Replace(strData, E)
'This section replaces "himself" with "herself"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = U
End With
RE6 = RE.Replace(strData, F)
'This section replaces "His" with "Her"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = V
End With
RE6 = RE.Replace(strData, G)
'This section replaces "his" with "her"
With RE
.MultiLine = True
.Global = True
.IgnoreCase = False
.Pattern = W '
RE6 = RE.Replace(strData, H)
End With
End Function
当我在这段文本上运行此代码时:
James has settled effortlessly in his new class. He has shown seriousness and demonstrated traits of a serious student in the first half of the term. I am very optimistic that his positive attitude towards work, if he does not relent, will yield positive dividends. However, James needs to respond positively to prompts on getting himself better organised in school. I wish Him, him the best in the second half of the term.
我只会把“他的”换成“她的”。如果我注释掉最后一点,那么我只会将“他”替换为“她”。任何帮助都将受到欢迎。