Option Explicit
'
Public wbkRapport As Excel.Workbook
Public wbRapportErreur As Excel.Workbook
Public wbConformite As Excel.Workbook
Public wbFicCSV As Workbook
Public NbreOccurrences, Avancement As Integer
Public Sub Main()
Dim derlig, derligne As Integer
Dim i, j As Long
Dim cell, cellule, cheminCSV, ficCSV As Variant
Dim erreurDeltaX As Boolean
Application.ScreenUpdating = False
Set wbConformite = ActiveWorkbook
Set wbRapportErreur = Application.Workbooks.Open("Y:\Suivi Prod\UFM160\Data TIMON\Rapport pb DeltaX.xlsx")
With wbRapportErreur.Sheets(1)
derligne = .Range("A" & Rows.Count).End(xlUp).Row
End With
Call Lister_Fichiers
With wbkRapport.Worksheets(1)
Set cheminCSV = .Range("C2:C" & derligRapport)
End With
'- Nombre de fichiers
NbreOccurrences = 1 'Compteur correspondant aux occurrences de la boucle suivante
Set frmProgress = New frmStatus
Load frmProgress
frmProgress.Show
For Each cell In cheminCSV
erreurDeltaX = False
ficCSV = cell.Offset(0, -2).Value
Load frmProgress
frmProgress.Show
Avancement = Int((NbreOccurrences * 100) / (derligRapport - 1))
frmProgress.ProgressBar1.Value = (Avancement / 100) * (frmProgress.ProgressBar1.Max)
frmProgress.Label1.Caption = "Tests sur le fichier: " & ficCSV
frmProgress.Repaint
wbConformite.Activate
wbConformite.Sheets(1).Cells.Delete shift:=xlUp
' On charge sur la feuille principale les données du fichier CSV
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & cell _
, Destination:=Range("$A$1"))
'.CommandType = 0
.Name = "CSV"
.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 = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
With wbConformite.ActiveSheet
derlig = .Range("A" & Rows.Count).End(xlUp).Row
i = 2
For j = 3 To derlig ' Première ligne à la dernière du fichier CSV
' Si le DeltaX est identique ou inférieur à celui de la ligne du dessous
If (wbConformite.Sheets(1).Range("A" & i).Value < wbConformite.Sheets(1).Range("A" & j).Value) Or (wbConformite.Sheets(1).Range("A" & i).Value = wbConformite.Sheets(1).Range("A" & j).Value) Then
i = i + 1
GoTo suivant
Else: wbRapportErreur.Sheets(1).Range("B" & derligne + 1) = i
wbRapportErreur.Sheets(1).Range("A" & derligne + 1) = ficCSV
derligne = derligne + 1
erreurDeltaX = True
GoTo suivant
End If
suivant: '-- mise à jour formulaire de progression
Load frmProgress
frmProgress.Show
frmProgress.LblStatus.Caption = "Traitement de la ligne : " & i & " sur " & derlig
frmProgress.Repaint
Next j
If erreurDeltaX = False Then
wbRapportErreur.Sheets(1).Range("A" & derligne + 1) = ficCSV
wbRapportErreur.Sheets(1).Range("D" & derligne + 1) = "Fic OK !"
derligne = derligne + 1
End If
End With
NbreOccurrences = NbreOccurrences + 1 'On incrémente le compteur
Next
Load frmProgress
frmProgress.Show
frmProgress.ProgressBar1.Value = (Avancement / 100) * (frmProgress.ProgressBar1.Max)
frmProgress.Label1.Caption = "!! TESTS TERMINES !!"
frmProgress.LblStatus.Caption = "Fin"
frmProgress.Repaint
'On fait une pause dans le code pour montrer la complétion de la waitingBarre à 100%
Application.Wait Time + TimeSerial(0, 0, 5)
frmProgress.Hide
Unload frmProgress
wbConformite.Sheets(1).Cells.Delete shift:=xlUp
End Sub