3

I want to split a certain string and store them in a variable.

  GetID = "13H4"
  sSplit = Split(GetID)

I want to split them and store "13" in a variable and "4" in another variable.

I don't want to use MID,RIGHT,LEFT methods because I don't want to put a location of string to get "13" or "4". Do you guys know how to do it?

Thanks guys.

1 Answer 1

13

Here are a few possibilities to show you how you can do that :

Dim var1 As String, _
    var2 As String, _
    sSplit() As String
  GetID = "13H4"

  sSplit = Split(GetID, "H")
  var1 = sSplit(0)
  var2 = sSplit(1)

'--- OR ---

  var1 = Split(GetID, "H")(0)
  var2 = Split(GetID, "H")(1)

If you want to output numbers directly and not a text of a number, this will be the way to do it :

Dim var1 As Integer, _
    var2 As Integer, _
    sSplit() As String

  GetID = "13H4"
  sSplit = Split(GetID, "H")
  var1 = CInt(sSplit(0))
  var2 = CInt(sSplit(1))
2
  • Awesome. Thanks for the complete explanation @R3uK. Appreciate your help
    – bigbryan
    Commented Sep 30, 2015 at 9:37
  • Glad I could help! ;) If your expressions to split become much more complex, you may want to use regular expressions, see how to do it in the 2 big answers in this post : stackoverflow.com/questions/22542834/…
    – R3uK
    Commented Sep 30, 2015 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.