我正在使用Excel VBA中的string
中的一些IBAN / SWIFT代码,但我想从字符串中选择IBAN编号。
样品:
DK4520005010201483 IBAN NDEADKKKXXX
NL24ABNA0424079763 iban ABNANL2A
DE56300700100303808000 IBAN DEUTDEDD
上面的字符串保存在字符串iban
中
代码:
Dim cutDownStr as String, iban as string
cutDownStr = iban
fooStr = Left(cutDownStr, (Len(cutDownStr) - InStr(cutDownStr, " ")))
MsgBox fooStr
答案 0 :(得分:2)
如果您想要的只是 iban 之前的子字符串,那么:
Sub qwerty()
iban = "DK4520005010201483 IBAN NDEADKKKXXX"
foostr = Split(iban, " ")(0)
MsgBox foostr
End Sub
答案 1 :(得分:2)
您的问题是您的代码正在从整个字符串中减少IBAN的长度。您需要在IBAN之后剪切字符串的长度,包括空格。你可以用:
来做到这一点fooStr = Left(cutDownStr, (Len(cutDownStr) - (Len(cutDownStr) - InStr(cutDownStr, " "))))
答案 2 :(得分:1)
使用功能
Function findIban(iban as String)
fooStr = split(iban," ")
findIban = fooStr(0)
End Function
<强>用法强>
Dim cutDownStr as String
cutDownStr = "DK4520005010201483 IBAN NDEADKKKXXX"
fooStr = findIban(cutDownStr)
MsgBox fooStr
答案 3 :(得分:0)
如果您的String没有很好地分隔,InstrRev是一个很酷的函数,例如,IBAN部分有时是10个char,有时是8个char。 InstrRev像Instr那样为你提供字符数(从左边开始),但它从右边搜索字符串。在“/ John / Son”中搜索“/”时,结果是6 NOT 1.从那里你可以使用正确的功能。
Sub IBANiac
Dim IBAN1, FindIBAN as String
IBAN1 = "DK4520005010201483 IBAN NDEADKKKXXX"
findIBAN = right(IBAN1,len(IBAN1)-instrrev(_
IBAN1,"IBAN ",-1,vbtextcompare))
msgbox findIBAN
'May want to add a +-1 to the second part of the right _
function to account for spacing
End Sub