Excel VBA返回字符串的大写部分
我正在尝试创建一个函数,该函数将从较长的大小写混合字符串返回大写部分 我有以字符串形式输入的地址,以郊区为大写,并希望提取郊区和邮政编码 e、 g 请记住,有些郊区是两个词,例如“THIS郊区2222” 这是我到目前为止所拥有的,但它不起作用。 任何帮助都将不胜感激Excel VBA返回字符串的大写部分,excel,vba,Excel,Vba,我正在尝试创建一个函数,该函数将从较长的大小写混合字符串返回大写部分 我有以字符串形式输入的地址,以郊区为大写,并希望提取郊区和邮政编码 e、 g 请记住,有些郊区是两个词,例如“THIS郊区2222” 这是我到目前为止所拥有的,但它不起作用。 任何帮助都将不胜感激 Function Suburb(strInput As String) As String Dim strTest As String Dim strEnd As Integer Dim n As Integer, counter
Function Suburb(strInput As String) As String
Dim strTest As String
Dim strEnd As Integer
Dim n As Integer, counter As Integer
'Count the characters in the string and set variable to string length - 5 (for postcode)
strSubEnd = Len(strInput) - 5
counter = 1
'Start Loop
For n = 1 To strEnd
'start from First Character and move to first space
n = InStr(counter, strInput, " ")
'create sting to test
strTest = Mid(strInput, n + 1, strSubEnd - n)
'check if string is upper case
If strTest = UCase(strTest) Then
Suburb = Mid(strInput, n + 1, Len(strInput) - n)
'Else increase counter and re-test
Else: counter = n
End If
Next
End Function
使用“拆分”拆分空格上的字符串,并测试它是否不是数字和大写:
Function Suburb(strInput As String) As String
Dim i As Integer
Dim spltStr() As String
spltStr = Split(strInput)
For i = 0 To UBound(spltStr)
If UCase(spltStr(i)) = spltStr(i) And Not IsNumeric(spltStr(i)) Then
Suburb = Suburb & " " & spltStr(i)
End If
Next i
Suburb = Trim(Suburb) & Right(strInput, 5)
End Function
我的正则表达式方法
公共函数(strInput作为字符串)作为字符串
Dim RegExp作为对象
设置RegExp=CreateObject(“VBScript.RegExp”)
使用RegExp
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=“([A-Z]+)\s)?([A-Z]+)\s([0-9]+)”
With.Execute(strInput)
如果是,那么数到0
郊区=.Item(0).Value
如果结束
以
以
Set RegExp=Nothing
端函数
谢谢Scott是否有一种简单的方法可以修改它以将街道段作为另一个函数返回?只需使用替换,将B1中的内容替换为空字符串“”
我已使用以下代码将C1单元功能街中的“01 street St”(strInput作为字符串,郊区作为字符串)返回为string street=Replace(strInput,郊区,“”)结束函数Scott,我遇到了“PO Box”的问题;PO在郊区拾取,因为它是大写的。有什么建议吗?我的原始代码结构试图从左到右工作,测试字符串部分的大写字母,但无法使其工作。上述问题通过:for I=0 to UBound解决(spltStr)如果UCase(spltStr(i))=spltStr(i)而不是IsNumeric(spltStr(i))而不是UCase(spltStr(i))=PO,则郊区=郊区&“”&spltStr(i)结束,如果下一个iI使用Scott提供的解决方案遇到其他问题(见下文)因此,我试图用我的原始代码解决这些问题。我认为问题在我用来遍历字符串的循环中,因为当我测试For和Next之间的每一行时,我得到了期望的结果。
Function Suburb(strInput As String) As String
Dim i As Integer
Dim spltStr() As String
spltStr = Split(strInput)
For i = 0 To UBound(spltStr)
If UCase(spltStr(i)) = spltStr(i) And Not IsNumeric(spltStr(i)) Then
Suburb = Suburb & " " & spltStr(i)
End If
Next i
Suburb = Trim(Suburb) & Right(strInput, 5)
End Function
Dim strTest As String
Dim strEnd As Integer
Dim n As Integer
'Count the characters in the string and set variable to string length - 5 (for postcode)
strEnd = Len(strInput) - 5
counter = 1
'Start Loop
For n = 1 To strEnd
'start from First Character and check if it is a space
If Mid(strInput, n, 1) = " " Then
'create sting to test
strTest = Mid(strInput, n + 1, strEnd - n)
'check if string is upper case
If strTest = UCase(strTest) Then
Suburb2 = Mid(strInput, n + 1, Len(strInput) - n)
Exit For
'Else move to next character and re-test
End If
End If
Next n
End Function
Public Function Suburb(strInput As String) As String
Dim RegExp As Object
Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "(([A-Z]+)\s)?([A-Z]+)\s([0-9]+)"
With .Execute(strInput)
If .Count <> 0 Then
Suburb = .Item(0).Value
End If
End With
End With
Set RegExp = Nothing
End Function