|
Public Function strReplace(ByVal sText As String) As String
Dim reg
Dim zmMatches As String
Dim szMatches As String
Dim spansCount As Integer
spansCount = ActiveWorkbook.Worksheets("概况表").Cells(1, 2).Value
Set reg = CreateObject("vbscript.regexp")
'sText = Worksheets("汇总表").Range("a1").Value
With reg
'设置是否匹配所有的符合项,True表示匹配所有, False表示仅匹配第一个符合项
.Global = True
'设置是否区分大小写,True表示不区分大小写, False表示区分大小写
.IgnoreCase = True
.Pattern = "^[A-Za-z]+"
'判断是否可以找到匹配的字符,若可以则返回True
'MsgBox .test(sText)
If .test(sText) Then
'对字符串执行正则查找,返回所有的查找值的集合,若未找到,则为空
Set objMatches = .Execute(sText)
zmMatches = LCase(objMatches(0).Value) '编号从0开始
End If
'把字符串中用正则找到的所有匹配字符替换为其它字符
'MsgBox .Replace(sText, "")
End With
With reg
'设置是否匹配所有的符合项,True表示匹配所有, False表示仅匹配第一个符合项
.Global = True
'设置是否区分大小写,True表示不区分大小写, False表示区分大小写
.IgnoreCase = True
.Pattern = "[0-9]+$"
'判断是否可以找到匹配的字符,若可以则返回True
'MsgBox .test(sText)
If .test(sText) Then
'对字符串执行正则查找,返回所有的查找值的集合,若未找到,则为空
Set objMatches = .Execute(sText)
szMatches = objMatches(0).Value '编号从0开始
End If
'把字符串中用正则找到的所有匹配字符替换为其它字符
'MsgBox .Replace(sText, "")
End With
Select Case zmMatches
Case "k"
strReplace = "第" & CStr(szMatches) & "跨"
Case "dt"
If szMatches = 0 Then
strReplace = CStr(szMatches) & "#桥台"
ElseIf szMatches = spansCount Then
strReplace = CStr(szMatches) & "#桥台"
Else
strReplace = CStr(szMatches) & "#桥墩"
End If
Case "z"
strReplace = CStr(szMatches) & "#支座"
Case "b"
strReplace = CStr(szMatches) & "#主梁"
Case Else
strReplace = sText
End Select
'MsgBox strReplace
End Function
|
|