我只想选择括号中的字符"()"我发现代码将在同一个字符串之间选择字符:
Public Function GetStuffYouWant(ByVal pInput As Variant, _
Optional pSplitChar As String = "-") As Variant
Dim varResult As Variant
Dim varPieces As Variant
If IsNull(pInput) Then
varResult = Null
Else
varPieces = Split(pInput, pSplitChar)
If UBound(varPieces) > 1 Then
varResult = varPieces(1)
Else
varResult = Null
End If
End If
GetStuffYouWant = varResult
End Function
它很有效,因为当存在空值时,我不会出现空错误。
麻烦的是我需要在两个已知字符串之间选择字符。我发现这个代码看了两个字符串,但我不知道如何将它写入第一个代码以获得我想要的结果:
dim first as integer
dim second as integer
dim result as string
first = instr(1,"yourtext","-")
second = instr(first+1,"yourtext","-")
if first > 0 and second > first then
second = second - first
result = mid("yourtext",first+1, second-1)
end if
这是我需要的一个例子:
Before: I need:
Issue Issue
------ ------
1 (Dog) at the carpet Dog at the carpet
2 <---Not a null error
3 (Cat) dog Cat
答案 0 :(得分:0)
此代码将解析“varPieces(1)”中“(”和“)”
内的文本Public Function GetStuffYouWant(ByVal pInput As Variant, _
Optional pSplitChar As String = "-") As Variant
Dim varResult As Variant
Dim varPieces As Variant
If IsNull(pInput) Then
varResult = Null
Else
varPieces = Split(pInput, pSplitChar)
If UBound(varPieces) > 1 Then
varResult = GetStuffYouWant_Parse(varPieces(1))
Else
varResult = Null
End If
End If
GetStuffYouWant = varResult
End Function
Public Function GetStuffYouWant_Parse(ByVal pInput As String) As String
dim first as integer
dim second as integer
dim result as string
first = instr(1,pInput ,"(")
second = instr(first+1,pInput,")")
if first > 0 and second > first then
second = second - first
result = mid(pInput,first+1, second-1)
end if
GetStuffYouWant_Parse = result
End Function