Вот код, который, вероятно, будет делать то, что вам нужно:
Sub determine_delim()
Dim filetocheck As String, firstline As String
Dim ff As Long
filetocheck = "full path and name of file here"
ff = FreeFile
Open filetocheck For Input As ff
Line Input #ff, firstline
Close ff
delimiter = most_popular(firstline)
End Sub
Function most_popular(str As String)
Dim pieces As Variant
Dim cnt As Long, ch as Long
Dim minCount As Long
Dim possibles As String
possibles = "|¦,;" & Chr(9) ' Chr(9)=Tab
For ch = 1 To Len(possibles)
pieces = Split(str, Mid(possibles, ch, 1))
cnt = UBound(pieces)
If minCount < cnt Then
minCount = cnt
most_popular = Mid(possibles, ch, 1)
End If
Next ch
End Function
Вместо того, чтобы просто найти самый популярный символ в первой строке - код ищет наиболее популярный символ в строке, которая также находится в possibles
- которую я предварительно загрузил с наиболее распространенными разделителями, которые вы найдете в плоских файлах - запятая, труба, сломанная труба и таб. Это предотвращает включение таких вещей, как подчеркивание или пробелы (обычно встречающиеся в строках заголовка).