'#####################################################################################################################
'# Sélection du fichier #
'#####################################################################################################################
Public Sub LireFichierTxt_Ref(ByRef intBouton As Long)
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim fd As FileDialog
Dim repertoire As String
Dim nomFichier As String
Dim Fichier As File
Dim fso As New FileSystemObject
Dim vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker) 'Création d'un Objet FileDialog en tant que boîte de diag File Picker (sélectionneur de fichiers)
fd.AllowMultiSelect = False 'On ne peut choisir qu'un seul fichier
fd.Title = "Sélectionnez un fichier txt" 'Pour renommer la boîte de dialogue par défaut
début:
With fd
If .Show = -1 Then 'La méthode Show permet d'afficher la boîte de diag de sélection de fichier
For Each vrtSelectedItem In .SelectedItems 'Step through each string in the FileDialogSelectedItems collection.
nomFichier = vrtSelectedItem
If Right(vrtSelectedItem, 4) = ".txt" Then 'La macro doit reconnaître les fichiers en .txt
TraiterFichier_Ref fso.GetFile(nomFichier) 'il effectue la transformation ci-dessous
Else
MsgBox "Vous devez sélectionner un fichier txt '.txt'"
GoTo début
End If
Next vrtSelectedItem
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
Application.DisplayAlerts = True
End Sub
'#####################################################################################################################
'# Ouvre un fichier texte, sépare chaque colonne par ses ";" et le met dans un fichier excel #
'#####################################################################################################################
Sub TraiterFichier_Ref(ByRef Fichier As File)
Dim Wb As Workbook
Dim ws As Worksheet
Dim chemin As String
Dim nomFichier As String
Dim numFic As Integer
Dim i, iCol, iRow As Long
Dim j As Long
Dim ligneFichier As String
Dim ligneFichierSplit() As String
Dim Separateur As String
Dim nom_feuille_Ref As String
Dim MyStr As String
Dim MoyenneRef As Double 'déclare la variable MoyenneRef
Set Wb = Application.Workbooks.Add 'on ouvre un nouveau classeur
nom_feuille_Ref = ActiveWorkbook.name
Set ws = Wb.Worksheets(1) 'la première feuille
numFic = FreeFile
Separateur = ";" 'Le séparateur des colonnes est un caractère "|"
Open Fichier.Path For Input As #numFic 'On ouvre le fichier txt
indexDébut = 0
iRow = 1
Do While Not EOF(numFic)
iCol = 1
Line Input #numFic, ligneFichier 'On récupère la ligne suivante
ligneFichierSplit = Split(ligneFichier, Separateur) 'On sépare la ligne dans un tableau
For i = LBound(ligneFichierSplit) To UBound(ligneFichierSplit) 'On copie le tableau
ws.Cells(iRow, iCol) = ligneFichierSplit(i)
iCol = iCol + 1
Next
iRow = iRow + 1
Loop
indexFin = iRow
'MsgBox nom_feuille_Ref
Close #numFic 'fermeture du fichier texte
Set O = Sheets("Feuil1") 'définit l'onglet O
DL = O.Cells(Application.Rows.Count, 2).End(xlUp).Row 'définit la dernière ligne éditée DL de la colonne 2 (=B) de l'onglet O
Set PL = O.Range("B1:B" & DL) 'définit la plage PL
MoyenneRef = Application.WorksheetFunction.Sum(PL) / DL 'définit la moyenne M
MsgBox MoyenneRef 'affiche la moyenne M
End Sub