Private Sub Workbook_Open()
Call import_csv
End Sub
Option Explicit
Sub import_csv()
Dim m_fichier As Variant, vrtFichier As Variant
Dim intStart As Integer, intEnd As Integer
Dim strNom As String
'Choix du ou des fichiers(tu peux faire le traitement de plusieurs fichiers a la fois en les sélectionnant tous en même temps)
m_fichier = Application.GetOpenFilename('CSV Files (*.csv),*.csv', , 'Ouverture fichier Remonté de Caisse', , True)
Application.ScreenUpdating = False
For Each vrtFichier In m_fichier
' ajout d 'un nouveau fichier (pour ne pas copier tout les fichier traiter ds ce fichier-ci)
Workbooks.Add
With ActiveSheet.QueryTables.Add(Connection:='TEXT;' & vrtFichier, Destination:=Range('A1'))
intStart = InStrRev(vrtFichier, '\\', -1) + 1
intEnd = InStr(1, vrtFichier, '.')
strNom = Mid(vrtFichier, InStrRev(vrtFichier, '\\', -1) + 1, intEnd - intStart)
.Name = strNom
.PreserveFormatting = True
.RefreshStyle = xlInsertDeleteCells ' xlInsertEntireRows
.SaveData = True
'.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileSemicolonDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 9)
.TextFileDecimalSeparator = '.'
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Call Ajout_entête_date(strNom)
Call trie_col_A
Next vrtFichier
Application.ScreenUpdating = True
End Sub
Sub trie_col_A()
Range(Cells(2, 1), Cells(Cells(65536, 1).End(xlUp).Row, 4)).Select
Selection.Sort Key1:=Range('A1'), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
Columns('A😀').EntireColumn.AutoFit
With Range('A1😀1').Interior
.ColorIndex = 44
.Pattern = xlSolid
End With
Cells(1, 1).Select
End Sub
Public Sub Ajout_entête_date(strDateRef As String)
Dim strDateTemp As String
Dim strDate As Date
With ActiveWorkbook.Sheets(1)
.Rows('1:1').Insert Shift:=xlDown
.Cells(1, 1) = 'Produit'
.Cells(1, 2) = 'Quantité'
.Cells(1, 3) = 'Prix'
.Cells(1, 4) = 'Date'
strDateTemp = Right(strDateRef, 8)
strDate = CDate(Mid(strDateTemp, 1, 2) & '-' & Mid(strDateTemp, 3, 2) & '-' & Mid(strDateTemp, 5, 8))
.Range(.Cells(2, 4), .Cells(.Cells(65000, 1).End(xlUp).Row, 4)).Value = strDate
.Range(.Cells(2, 3), .Cells(.Cells(65000, 1).End(xlUp).Row, 3)).NumberFormat = '_-* #,##0.00 ''€''_-;-* #,##0.00 ''€''_-;_-* ''-''?? ''€''_-;_-@_-'
End With
End Sub