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.

0m3r
  • 12,286
  • 15
  • 35
  • 71
bigbryan
  • 411
  • 6
  • 19
  • 36

1 Answers1

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))
R3uK
  • 14,417
  • 7
  • 43
  • 77
  • Awesome. Thanks for the complete explanation @R3uK. Appreciate your help – bigbryan Sep 30 '15 at 09: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 : http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops – R3uK Sep 30 '15 at 09:42