我只想选择在括号“()”之间找到的字符,我发现将在同一字符串之间选择字符的代码:
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
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