1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Function DevinerSeparateur(sFile As String) As String
Dim lgTabCnt As Long, lgCommaCnt As Long, lgSemiColCnt As Long
Dim l As Long, lgMaxCnt As Long, sResult As String
Dim iFic As Integer, iRowCnt As Integer
Dim sText As String, sOneRow As String
On Error GoTo ErrH
' ouvrir fichier
iFic = FreeFile()
Open sFile For Input As iFic
' Lire les 10 1ères lignes
Do While (Not EOF(iFic)) And (iRowCnt < 10)
iRowCnt = iRowCnt + 1
Line Input #iFic, sOneRow
sText = sText + sOneRow
Loop
Close iFic
' Compter séparateurs potentiels
For l = 1 To Len(sText)
Select Case Mid(sText, l, 1)
Case vbTab: lgTabCnt = lgTabCnt + 1
Case ",": lgCommaCnt = lgCommaCnt + 1
Case ";": lgSemiColCnt = lgSemiColCnt + 1
End Select
Next
' Compteur Maxi
If lgTabCnt > lgMaxCnt Then lgMaxCnt = lgTabCnt
If lgCommaCnt > lgMaxCnt Then lgMaxCnt = lgCommaCnt
If lgSemiColCnt > lgMaxCnt Then lgMaxCnt = lgSemiColCnt
' Caractère correspondant au compteur maxi
If lgMaxCnt = lgTabCnt Then
sResult = vbTab
ElseIf lgMaxCnt = lgCommaCnt Then
sResult = ","
ElseIf lgMaxCnt = lgSemiColCnt Then
sResult = ";"
Else
sResult = ""
End If
Sortie:
Close iFic
' Retourner séparateur trouvé ou chaîne vide sinon.
DevinerSeparateur = sResult
Exit Function
ErrH:
MsgBox "Erreur No. " & Err.Number & " : " & Err.Description
Resume Sortie
End Function |
Partager