Vinoth
Vinoth

Reputation: 1

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.

Upvotes: 0

Views: 163

Answers (2)

Jules
Jules

Reputation: 1423

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, " ")

Upvotes: 1

Jean-Pierre Oosthuizen
Jean-Pierre Oosthuizen

Reputation: 2693

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

Upvotes: 4

Related Questions