title: How to extract numbers from a text string in VBA
body: <p>I need to extract the numbers from a string of text and I'm not quite sure how to do it.  The code I've attached below is very preliminary and most likely can be done more elegantly.  A sample of the string I'm trying to parse is as follows:</p>
  
  <p>"ID CSys ID Set ID Set Value Set Title 7026..Plate Top MajorPrn Stress 7027..Plate Top MinorPrn Stress 7033..Plate Top VonMises Stress"</p>
  
  <p>I need to pull the numbers 7026, 7027, and 7033.  The string will vary in length and the number of values that I'll need to pull will also vary.  Any help would be much appreciated.  Thanks!</p>
  
  <pre><code>Dim WrdArray() As String
  Dim txtstrm As TextStream
  Dim line As String
  Dim clm As Long
  Dim Rw As Long
  </code></pre>
  
  <p>'------------------------------------------------------------</p>
  
  <pre><code>Dim i As Long
  
  Dim strPath As String
  Dim strLine As String
  Dim count, count1 As Integer
  Dim holder As String
  Dim smallSample As String
  
  count = 0
  count1 = 1
  holder = ""
  
  'Remove Filters and Add Custom Filters
  Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
  Call Application.FileDialog(msoFileDialogOpen).Filters.Add("Text Files", "*.txt")
  Call Application.FileDialog(msoFileDialogOpen).Filters.Add("Dat Files", "*.dat")
  
  'only allow the user to select one file
  Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
  
  'make the file dialog visible to the user
  intChoice = Application.FileDialog(msoFileDialogOpen).Show
  
  'determine what choice the user made
  If intChoice &lt;&gt; 0 Then
  
  'get the file path selected by the user
  strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
  
  End If
  </code></pre>
  
  <p>'------------------------------------------------------------</p>
  
  <pre><code>If strPath &lt;&gt; "" Then
      Set txtstrm = FSO.OpenTextFile(strPath)
  Else
      MsgBox "No file selected.", vbExclamation
      Exit Sub
  End If
  
  Rw = 1
  Do Until txtstrm.AtEndOfStream
    line = txtstrm.ReadLine
    clm = 1
    WrdArray() = Split(line, " ") 'Change with ; if required
  
    For Each wrd In WrdArray()
       If Rw = 1 Then
          Do While count &lt;&gt; Len(wrd)
              smallSample = Left(wrd, 1)
              If smallSample = "0" Or smallSample = "1" Or smallSample = "2" Or smallSample = "3" Or smallSample = "4" _
                      Or smallSample = "5" Or smallSample = "6" Or smallSample = "7" Or smallSample = "8" _
                      Or smallSample = "9" Then
                  holder = holder &amp; smallSample
              Else
                  If holder &lt;&gt; "" Then
                      Cells(count1, 1) = holder
                      count1 = count1 + 1
                  End If
                  holder = ""
              End If
              wrd = Right(wrd, Len(wrd) - 1)
              clm = clm + 4
              ActiveSheet.Cells(Rw, clm) = holder
          Loop
       Else
          ActiveSheet.Cells(Rw, clm) = wrd
          clm = clm + 1
       End If
    Next wrd
    Rw = Rw + 1
  Loop
  txtstrm.Close
  </code></pre>
  
  <p>End Sub</p>