OVH Cloud OVH Cloud

Nommer nouvelle feuille à parir d'un fichier

5 réponses
Avatar
fromto
Bonjour,
J'importe un fichier texte à partir d'excel en VBA;
Je souhaiterai enrigistrer le enregistrements dans une nouvelle feuille
portant le nom du fichier que je recupère à partir de GetOpenFilename ?
merci.

5 réponses

Avatar
MichDenis
Bonjour Fromto,

Directement de l'aide d'excel :

dim filetoOpen as variant
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.Open fileToOpen
End If


Salutations!



"fromto" a écrit dans le message de news:
Bonjour,
J'importe un fichier texte à partir d'excel en VBA;
Je souhaiterai enrigistrer le enregistrements dans une nouvelle feuille
portant le nom du fichier que je recupère à partir de GetOpenFilename ?
merci.
Avatar
fromto
Merci,
Mais ce n'est pas tout à fait cela que je cherche, je voudrais ouvrir le
fichier et nommer nouvelle feuille dans le même classeur.



"MichDenis" a écrit dans le message de news:
%
Bonjour Fromto,

Directement de l'aide d'excel :

dim filetoOpen as variant
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.Open fileToOpen
End If


Salutations!


Avatar
MichDenis
Bonjour Fromto,

voici une procédure et commentaire émanant d'un MVP du forum anglophone sur Excel : M. Pearson

Tu me permettras de ne pas te traduire les explications qu'il donne ....en français !

Pour ton bénéfice, tu insères ces 2 procédures dans un module standard, et tu appelles celle-ci :

Public Sub DoTheImport


'Excel does not give you a method to import
'a text file directly into an existing worksheet.
'The Text Import Wizard will always create a new
'workbook. The macro that follows allows you to
'import a delimited text file directly into the
'active worksheet, beginning in the active cell.
'The macro ImportTextFile will import the file
'specified by FName into the active worksheet.
'Imported values begin in the active cell, and each
'line of the text file are placed on the same row
'of the worksheet. Each subsequent line of the text
'file is place on the next row of the worksheet.
'Imported values, separated by the character specified
'by Sep, are placed in separate columns. Each line
'in the text file does not need to contain the same
'number of values. If there is a blank line in the text
'file, a blank row will appear in the worksheet.

'-----------------------------------------------
Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

'-----------------------------------------------
'You can call this macro from another VBA
'procedure as follows:
'ImportTextFile "c:temptest.txt", ";"

'Since this code has parameters, it will not appear
'in the standard "Macros" dialog list (ALT+F8).
'The following procedure will prompt you for the
'filename and delimiter character, and then run the
'ImportTextFile procedure.

Public Sub DoTheImport()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub
'-----------------------------------------------


Salutations!





"fromto" a écrit dans le message de news:
Merci,
Mais ce n'est pas tout à fait cela que je cherche, je voudrais ouvrir le
fichier et nommer nouvelle feuille dans le même classeur.



"MichDenis" a écrit dans le message de news:
%
Bonjour Fromto,

Directement de l'aide d'excel :

dim filetoOpen as variant
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.Open fileToOpen
End If


Salutations!


Avatar
fromto
Merci, je l'avais trouvé aussi.
J'ai dû la modifier un peu car dans mes enregistrements, il n'y a pas de
séparateur, j'ai résolu le problème. Par contre la Feuille n'est toujours
pas nommé avec le nom de l'import, et c'est cela qui me gêne le plus.

Cordialement

"MichDenis" a écrit dans le message de news:

Bonjour Fromto,

voici une procédure et commentaire émanant d'un MVP du forum anglophone
sur Excel : M. Pearson

Tu me permettras de ne pas te traduire les explications qu'il donne ....en
français !

Pour ton bénéfice, tu insères ces 2 procédures dans un module standard, et
tu appelles celle-ci :

Public Sub DoTheImport


'Excel does not give you a method to import
'a text file directly into an existing worksheet.
'The Text Import Wizard will always create a new
'workbook. The macro that follows allows you to
'import a delimited text file directly into the
'active worksheet, beginning in the active cell.
'The macro ImportTextFile will import the file
'specified by FName into the active worksheet.
'Imported values begin in the active cell, and each
'line of the text file are placed on the same row
'of the worksheet. Each subsequent line of the text
'file is place on the next row of the worksheet.
'Imported values, separated by the character specified
'by Sep, are placed in separate columns. Each line
'in the text file does not need to contain the same
'number of values. If there is a blank line in the text
'file, a blank row will appear in the worksheet.

'-----------------------------------------------
Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

'-----------------------------------------------
'You can call this macro from another VBA
'procedure as follows:
'ImportTextFile "c:temptest.txt", ";"

'Since this code has parameters, it will not appear
'in the standard "Macros" dialog list (ALT+F8).
'The following procedure will prompt you for the
'filename and delimiter character, and then run the
'ImportTextFile procedure.

Public Sub DoTheImport()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub
'-----------------------------------------------


Salutations!





"fromto" a écrit dans le message de news:

Merci,
Mais ce n'est pas tout à fait cela que je cherche, je voudrais ouvrir le
fichier et nommer nouvelle feuille dans le même classeur.



"MichDenis" a écrit dans le message de news:
%
Bonjour Fromto,

Directement de l'aide d'excel :

dim filetoOpen as variant
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.Open fileToOpen
End If


Salutations!







Avatar
MichDenis
Bonjour FromTo,

Quand tu lances cette procédure :

L'importation dans la feuille se fait à partir de la cellule qui est sélectionnée au moment de lancer la procédure. Rien
ne t'empêche d'insérer une ligne de code te permettant de sélectionner la cellule de ton choix et de modifier le nom de
la feuille dans laquelle les données d'inscrivent :

'----------------------------------------
Public Sub DoTheImport()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If

'Ajouter
'********************
With ActiveSheet
.range("A1").Select
.name = FName
End With
'*********************
Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub
'----------------------------------------



Salutations!




"fromto" a écrit dans le message de news:
Merci, je l'avais trouvé aussi.
J'ai dû la modifier un peu car dans mes enregistrements, il n'y a pas de
séparateur, j'ai résolu le problème. Par contre la Feuille n'est toujours
pas nommé avec le nom de l'import, et c'est cela qui me gêne le plus.

Cordialement

"MichDenis" a écrit dans le message de news:

Bonjour Fromto,

voici une procédure et commentaire émanant d'un MVP du forum anglophone
sur Excel : M. Pearson

Tu me permettras de ne pas te traduire les explications qu'il donne ....en
français !

Pour ton bénéfice, tu insères ces 2 procédures dans un module standard, et
tu appelles celle-ci :

Public Sub DoTheImport


'Excel does not give you a method to import
'a text file directly into an existing worksheet.
'The Text Import Wizard will always create a new
'workbook. The macro that follows allows you to
'import a delimited text file directly into the
'active worksheet, beginning in the active cell.
'The macro ImportTextFile will import the file
'specified by FName into the active worksheet.
'Imported values begin in the active cell, and each
'line of the text file are placed on the same row
'of the worksheet. Each subsequent line of the text
'file is place on the next row of the worksheet.
'Imported values, separated by the character specified
'by Sep, are placed in separate columns. Each line
'in the text file does not need to contain the same
'number of values. If there is a blank line in the text
'file, a blank row will appear in the worksheet.

'-----------------------------------------------
Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

'-----------------------------------------------
'You can call this macro from another VBA
'procedure as follows:
'ImportTextFile "c:temptest.txt", ";"

'Since this code has parameters, it will not appear
'in the standard "Macros" dialog list (ALT+F8).
'The following procedure will prompt you for the
'filename and delimiter character, and then run the
'ImportTextFile procedure.

Public Sub DoTheImport()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub
'-----------------------------------------------


Salutations!





"fromto" a écrit dans le message de news:

Merci,
Mais ce n'est pas tout à fait cela que je cherche, je voudrais ouvrir le
fichier et nommer nouvelle feuille dans le même classeur.



"MichDenis" a écrit dans le message de news:
%
Bonjour Fromto,

Directement de l'aide d'excel :

dim filetoOpen as variant
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.Open fileToOpen
End If


Salutations!