2016-09-14 12 views
0

split()関数に複数の区切り文字を使用するにはどうすればよいですか?私はデリミタとして2つ以上の単語を使用したいが、それがどのように可能かは分かりません。vbaの複数の区切り文字(単語)

答えて

6

置き換えを使用して複数の単語を置き換えてから、分割して使用することができます。

あなたは次のように行くことができる

mystring= Replace(mystring, "hello", "#") 
mystring= Replace(mystring, "hi", "#") 
mystring= Replace(mystring, "thanks", "#") 
newstring= Split(mystring, "#") 
+0

を私はそれが一回の分割前の単語と一緒に区切り文字を引っ張ることができるようにしたいです。帰る方法はありますか? – johndoe253

+0

それを得られなかった。説明できますか? – Techidiot

+0

デリミタを引っ張りたい場合は、各置換後に "split"を繰り返してください。 – Stavm

1

は、次のとおりです。

Option Explicit 

Sub main() 
    Dim t As String 
    Dim arr As Variant, seps As Variant, sep As Variant 

    seps = Array("hello", "hi") '<--| define your seperators list 

    t = "hello i would hello like to hi split this string hello with multiple hello separators hi" '<--| string to split 

    t = " " & t & " " '<--| add trailing blank to catch possible "border" 'separators' 
    For Each sep In seps 
     t = Replace(t, " " & sep & " ", "|") 'turn all separators into one only 
    Next sep 
    t = Trim(t) '<--| remove trailing blanks 
    If Left(t, 1) = "|" Then t = Right(t, Len(t) - 1) '<--| remove any initial 'separator' 
    If Right(t, 1) = "|" Then t = Left(t, Len(t) - 1) '<--| remove any final 'separator' 
    arr = Split(t, "|") 

End Sub 
関連する問題