Bonjour,
J'ai une macro pour importer un fichier texte dans un tableau excel et j'aurai besoin que cette macro importe mon fichier texte à un endroit spécifique dans un onglet déjà existant d'un fichier excel. Actuellement la macro ouvre un nouveau fichier excel qu'elle nomme comme le fichier texte choisi pour l'importation.
Ci dessous la macro que j'ai récupéré et modifié pour récupérer un type de fichier différend ainsi que la page du site sur lequel je l'ai trouvé et en pièces jointes un exemple de log à traiter.
Sub ImportTextFileBackup()
'Import a text file using Excel's own import function.
Dim vFileName
On Error GoTo ErrorHandle
'The function GetOpenFileName gets the file name without
'opening the file.
'Here we use a filter to display only text files with "*.txt" as
'extension. If you omit the file filter, all files will show.
'Read the VBA help for other options.
vFileName = Application.GetOpenFilename("Text Files (*.log),*.log")
'If the user pressed Cancel or didn't select a text file, we exit.
If vFileName = False Or Right(vFileName, 3) <> "log" Then
GoTo BeforeExit
End If
'Switch screen updating off for speed.
Application.ScreenUpdating = False
'We now import the selected text file, and data is
'inserted into a new spreadsheet. If you want to use
'another delimiter than semicolon, you must change
'"Semicolon:=True" to "Semicolon:=False" and set the
'other delimiter (e.g. "Tab") to True.
'I recently discovered that you can avoid
'some formatting problems (e.g. with dates),
'if you add the finel "Local:=True". It depends
'on your local settings and Excel version, but
'the addition does no harm.
Workbooks.OpenText Filename:=vFileName, _
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, _
Other:=False, TrailingMinusNumbers:=True, _
local:=True
'Just to show how, we auto fit the width of column A.
Columns("A:A").EntireColumn.AutoFit
BeforeExit:
Application.ScreenUpdating = True
Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub
http://sitestory.dk/excel_vba/automatic-import-textfile.htm
Merci d'avances pour vos réponse