Maccro, Importer un fichier csv
Bonsoir,
J'ai un petit soucis au niveau d'excel :
Je fais supervision d'une machine, pour le moment toute les minutes j'enregistre son état dans un fichier csv ( marche arret defaut (1,2,3))
J'enregistre tout ceci dans un même fichier jusqu'à avoir 10080 donnée ( 1 semaines) puis je change de fichier.
Là j'essaye de crée une macro qui me permettrai d'importer ces fichiers ( je le sélectionne comme si j'ouvrais un fichier), je convertis le fichier pour l'avoir sous forme de tableau, et enfin j'actualise un tableau dynamique pour voir le % de temps d’arrêt marche etc...
Mon soucis ce situe au niveau de la sélection du fichier, je ne sais pas comment je peux sélectionner moi même un fichier grace à vba
Je vous montre mon code mais qui ne me permet que d'aller dans un fichier prédéfini, ce que j'aimerai c'est pouvoir avoir la liberté d'en choisir un autre sans entrer dans mon code :
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;G:\Manu\12062000.csv", Destination:=Range( _
"$A$1"))
.Name = "12062000_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("Feuil2").Select
Range("B7").Select
ActiveSheet.PivotTables("Tableau croisé dynamique1").PivotCache.Refresh
End SubJ'avais trouvé comment faire pour choisir l'ouverture d'un nouveau fichier par cette méthode :
Classeur = Application.GetOpenFilename("ClasseursExcel,*.*")
If Classeur = False Then Exit Sub
Workbooks.Open Filename:=Classeurmais je ne sais pas comment l'adapté pour mon importation
Merci pour votre aide
Bonne soirée
Salut H6Parmentier (j'adore le pseudo
Voici ton code au complet avec choix du fichier
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Function GetFileName(sFilter As String, sInitialDir As String, sTitle As String) As String
Dim OpenFile As OPENFILENAME, lReturn As Long
With OpenFile
.lStructSize = Len(OpenFile)
.lpstrFilter = sFilter
.nFilterIndex = 1
.lpstrFile = String(257, 0)
.nMaxFile = Len(OpenFile.lpstrFile) - 1
.lpstrFileTitle = OpenFile.lpstrFile
.nMaxFileTitle = OpenFile.nMaxFile
.lpstrInitialDir = sInitialDir
.lpstrTitle = sTitle
.flags = 0
End With
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
GetFileName = ""
Else
GetFileName = Trim(OpenFile.lpstrFile)
End If
End Function
Sub test()
Dim sPathFic As String, sFilter As String
sFilter = "Fichier d'export (*.csv)" & Chr(0) & "*.csv" & Chr(0)
' Donner le choix du fichier
sPathFic = GetFileName(sFilter, "G:\Manu", "Sélectionnez le fichier à ouvrir")
If sPathFic = "" Then Exit Sub
'
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & sPathFic, Destination:=Range("$A$1"))
.Name = "12062000_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("Feuil2").Select
Range("B7").Select
ActiveSheet.PivotTables("Tableau croisé dynamique1").PivotCache.Refresh
End SubA+
Hey oui, on aime ou on n'aime pas
ok! merci, je vais regarder tout ceci, si j'ai encore un soucis je re-posterais par la suite