Bonsoir Gaëlle,
Une proposition de lecture de fichier texte UTF-8 au moyen de l'object ADODB.Stream :
Option Explicit
Sub ImportTXT_UTF8()
Dim adoStream As Object
Dim oRange As Range
Dim aString() As String
Dim lRow As Long, lCol As Long, i As Long
Dim sTextFileToConvert As String
Dim lNbrows As Long
'Récupération du fichier texte UTF-8 à importer
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Show
.Title = "Sélectionner le fichier TXT UTF-8 à convertir"
If .SelectedItems.Count > 0 Then
sTextFileToConvert = .SelectedItems(1)
Else
MsgBox "Aucun fichier sélectionné!", vbOKOnly, "TRAITEMENT IMPOSSIBLE"
Exit Sub
End If
End With
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Charset = "UTF-8"
adoStream.LineSeparator = adLF
adoStream.Open
adoStream.LoadFromFile sTextFileToConvert
'Chargement de la ligne contenant les entêtes de colonnes
aString = Split(Replace(adoStream.ReadText(adReadLine), Chr(34), ""), ";") 'split des entêtes dans un tableau
lCol = UBound(aString)
If lCol > 0 Then
lRow = lRow + 1
With ActiveSheet
Set oRange = .Range(.Cells(lRow, 1), .Cells(lRow, lCol))
oRange.Value = Application.Transpose(Application.Transpose(aString))
End With
End If
'Chargement de la totalité restante du fichier texte en supprimant les "
aString = Split(Replace(adoStream.ReadText, Chr(34), ""), ";")
lNbrows = (UBound(aString) / lCol) - 1 ' Calcul du nombre de lignes du fichier texte chargé
With ActiveSheet
Set oRange = .Range(.Cells(2, 1), .Cells(2 + lNbrows, lCol))
oRange.Value = Application.Transpose(Application.Transpose(aString))
End With
adoStream.Close
Set adoStream = Nothing
End Sub