Salut Optimix,
Déjà on a une erreur dans le peu de code proposé :
Lire :
Sheets("SH_ACCUEIL").Activate
au lieu de :
Sheets(SH_ACCUEIL).Activate
Non pas d'erreur, puisqu'il y a une déclaration de constante (on ne savait pas avant d'avoir le fichier)
Const SH_ACCUEIL As String = "ACCUEIL"
Sinon pourquoi ne pas utiliser ce simple code
Sub LireFEC(sPath As String)
On Error GoTo ErrLire ' ? remplace le gestionnaire global
Dim ws As Worksheet
Dim WbTxt As Workbook
Set ws = Sheets(SH_FEC)
' Effacer donnees existantes en gardant les en-tetes (ligne 1)
Dim lastExist As Long
lastExist = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
If lastExist > 1 Then
ws.Range("A2:R" & lastExist).ClearContents
ws.Range("A2:R" & lastExist).Interior.ColorIndex = xlNone
End If
' Ouvrir le fichier texte comme un fichier Excel
Workbooks.OpenText Filename:=sPath, Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, TrailingMinusNumbers:=True
Set WbTxt = ActiveWorkbook
WbTxt.Sheets(1).Range([A2], ActiveCell.SpecialCells(xlLastCell)).Copy
ws.Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' Fermer le fichier
Application.DisplayAlerts = False
WbTxt.Close SaveChanges:=False
Application.DisplayAlerts = True
Exit Sub
ErrLire:
MsgBox "Erreur " & Err.Number & " à la ligne : " & Erl & vbCrLf & Err.Description, vbCritical
End Sub
Plutôt qu'une usine à gaz