title: Count commas in line of text file
body: <p>I have a text file and I'm trying to count the number of commas in the first line of the file using excel VB and then do an action if there are 3 - but something is wrong. When I use the replace method (commented out in this example below) the macro fails, and when I use the Split method it ALWAYS does the action, no matter what value I add in place of 3.</p>
  
  <pre><code>   'Load txt file into array
      Open FilePath For Input As #1
      dataArray = Split(Input$(LOF(1), #1), vbLf)
      Close #1
  
      'Test first line if it has three commas
      If Len(dataArray(0).value) - Len(Replace(dataArray(0).value, ",", "")) = 3 Then
      'If dataArray(0).Split(",").Length = 3 Then
  
     'Add comma to start of strings
          For i = LBound(dataArray) To UBound(dataArray)
  
              dataArray(i) = "," &amp; dataArray(i)
          Next i
  </code></pre>