Home Ask Login Register

Developers Planet

Your answer is one click away!

Vinoth February 2016

How to Split a string using VB Macro

I need to split this number 309701406 as 30 970 140 6. How can I do with VB macro script. Any one please look into this and assist.


Jean-Pierre Oosthuizen February 2016

AS mentioned in a comment to your Question, you can use the following:

SomeString = Mid(StringName, Start, Length)

Putting that into a Sub will look as follows:

 Option Explicit

 Sub SplitStringSub()

      Dim OriginalString As String

      Dim String1 As String
      Dim String2 As String
      Dim String3 As String
      Dim String4 As String

      OriginalString = 309701406

      'Mid wants a String input
      'Because of the need to split the Numerical String it is not explicity required to have the "  " around then String input line.
      'Should you want to Split a String of Alphabetical Characters the "  " will definitely be required.
      String1 = Mid(OriginalString, 1, 2)
      String2 = Mid(OriginalString, 3, 3)
      String3 = Mid(OriginalString, 6, 3)
      String4 = Mid(OriginalString, 9, 1)

 End Sub

Jules February 2016

If the 0 is delimiter you can do this.

Dim str As String
Dim aTmp() As String

str = "309701406"

aTmp() = Split(str, "0")
str = Join(aTmp, "0 ")
aTmp() = Split(str, " ")

Post Status

Asked in February 2016
Viewed 1,829 times
Voted 14
Answered 2 times


Leave an answer

Quote of the day: live life