1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| Option Explicit
Dim lastrow As Long
Dim ColNumber As Integer 'Nombre de colonne à sauver (les numéro des colonnes est sauvé dans la feuille Debug
Dim ColDocNo As Integer ' Numéro de colonnes des Doc No
Dim ColTFN As Integer ' Numéro de colonnes du transfer file name
Dim ColDocType As Integer ' Numéro de colonnes indiquant s'il s'agit d'un doc père ou d'une pièce jointe (calculé par la macro)
Dim ColIssueNo As Integer ' Numéro de colonnes indiquant le numéro d'issue
Dim ColDocNoOnly As Integer ' Numéro de colonnes indiquant le numéro du document sans l'issue (calculé par la macro)
Dim ColID As Integer ' Numéro de colonnes indiquant un ID unique pour chaque document propre ( calculé par la macro)
Dim ColSyncMacro As Integer ' Numéro de colonnes indiquant si le document est à prendre en compte ou est un doublon
Dim ColDoublonNo As Integer ' Numéro de colonnes indiquant un indifiant numéro de doublon
Dim BoolDoublon As Boolean ' Booléen qui indique la présence d'un doublon pour chaque comparaison de document
Dim BoolCCSRRTPL As Boolean ' Booléen indiquant qu'au moins un document nous concerne dans le doublon
Dim LineForTitle As Integer ' Nombre indiquant le nombre de lignes utilisées pour les titres
Dim DoublonNo As Long ' Identifiant du doublon
Dim DoublonLogNo As Long ' numéro de ligne du doublon pour sauvegarde
Dim Cell As Range
'Dim IDdic As Collection
'Dim IDdicAddress As Collection
Dim i, j, k, itemCount, numDoublon, testvar As Long
Dim fso, LogFile ' objects pour fichier de log
Sub Doublon()
'
' Macro de détection des doublons
' Par des colonnes Doc No et Issue No
''La feuille debug indique les colonnes qui seront recopiées dans la troisième feuille (feuille de travail)
' Dans ces colonnes il faut au minimum les colonnes:
' - Document No
' - Issue No
' - Transfer File name
' - Synchro macro
' pour que la macro de recherche des doublons fonctionne
'' Récupération des colonnes du DTTR vers une feuille de travail
LineForTitle = 2 '
DoublonLogNo = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile = fso.OpenTextFile("c:\test_macro\Doublon_Finder_log_file.txt", 2, True)
LogFile.write ("Doublon Finder")
LogFile.writeLine ("")
LogFile.writeLine Date & " " & DateTime.Time
Worksheets(1).Select
lastrow = Cells(1, 1).End(xlDown).Row
'Debug
Worksheets("Debug").Cells(1, 1) = "Nombre de ligne"
Worksheets("Debug").Cells(1, 2) = lastrow
Worksheets(2).Activate
ColNumber = Worksheets(2).Cells(1, 7).End(xlDown).Row
'MsgBox ColNumber
For i = 2 To ColNumber
'MsgBox ("i = " & i)
Worksheets(1).Activate
j = Worksheets("Debug").Cells(i, 7).Value
'MsgBox ("j = " & j)
Range(Cells(1, j), Cells(lastrow, j)).Copy
Worksheets(3).Activate
Cells(1, i - 1).Select
ActiveSheet.Paste
Next i
ColDocNoOnly = i - 1
'On repère le numéro de la colonne Document number
Worksheets(3).Activate
ColDocNo = Cells.Find("Document No").Column
'On recopie la colone Doc No et on supprime les "/"
Range(Cells(1, ColDocNo), Cells(lastrow, ColDocNo)).Copy
Cells(1, ColDocNoOnly).Select
ActiveSheet.Paste
Selection.Replace What:="/", Replacement:=""
Selection.Replace What:="-", Replacement:=""
Selection.Replace What:=" A", Replacement:="_A"
Selection.Replace What:=" B", Replacement:="_B"
ColDocType = ColDocNoOnly + 1
Cells(2, ColDocType).Value = "Doc type"
'On recherche les cellules qui ont un doc No qui s'apparente à une pièce jointe
For k = 3 To lastrow
'MsgBox Cells(k, ColDocType - 1)
If AttachedDoc1(Cells(k, ColDocType - 1)) = True Then
Cells(k, ColDocType).Value = "Pièce_jointe"
End If
Next k
'On repère la colonne Transfer file name
Worksheets(3).Activate
ColTFN = Cells.Find("Transfer File Name").Column
'MsgBox ColTFN
'On repère la colonne Issue No
Worksheets(3).Activate
ColIssueNo = Cells.Find("Issue No").Column
'MsgBox ColTFN
'On repère la colonne Synchro macro
Worksheets(3).Activate
ColSyncMacro = Cells.Find("Synchro macro").Column
'MsgBox ColSyncMacro
'On recherche dans la colonne transfert file name les lignes qui s'apparentent à une pièce jointe
For k = 3 To lastrow
'Cells(k, ColDocType).Interior.Color = RGB(0, 255, 0)
If AttachedDoc2(Cells(k, ColTFN)) = True Then
Cells(k, ColDocType).Value = "Pièce_jointe"
End If
Next k
'Remplissage d'un Colonne ID qui contient un identifiant unique pour chaque doc/issue
ColID = ColDocType + 1
Cells(2, ColID).Value = "ID"
For k = 3 To lastrow
'Si la Colonne Doc No ne contient pas de "_" => Doc père
If DocNoOnly(Cells(k, ColDocNoOnly)) = False Then
Cells(k, ColID).Value = Cells(k, ColDocNoOnly) & "i" & Cells(k, ColIssueNo)
Else
'Si contient "_" mais n'est pas une pièce jointe => Doc père
If Cells(k, ColDocType) <> "Pièce_jointe" Then
Cells(k, ColID).Value = FatherDocNoOnly(Cells(k, ColDocNoOnly)) & "i" & Cells(k, ColIssueNo)
End If
End If
Next k
'Recherche des doublons dans les documents peres
ColDoublonNo = ColID + 1
Cells(2, ColDoublonNo).Value = "Doublon Macro"
DoublonNo = 1
BoolDoublon = False
k = 3
While k <> lastrow
For i = (k + 1) To lastrow
'LogFile.writeLine ("Comparaison lignes " & k & " et " & i)
If Cells(k, ColID).Value = Cells(i, ColID).Value And Cells(i, ColDoublonNo).Value = "" Then
LogFile.writeLine ("Doublon ! Numéro " & DoublonNo)
Cells(k, ColDoublonNo).Value = DoublonNo
Cells(i, ColDoublonNo).Value = DoublonNo
BoolDoublon = True
End If
Next i
If BoolDoublon = True Then
DoublonNo = DoublonNo + 1
BoolDoublon = False
End If
k = k + 1
Wend
MsgBox "Wait"
k = 2
For j = 1 To DoublonNo
LogFile.writeLine ("Traitement doublon " & j)
BoolCCSRRTPL = False
If traitement_doublon(j) > 1 Then ' analyse si deux doc valides existant
BoolCCSRRTPL = ColorVisibleRows(LineForTitle) 'Coloration des lignes si deux doc valide existant et analyse si au moins un des document est taggué CCSR/RTPL
If BoolCCSRRTPL = True Then ' Si un des document est taggué on le log dans la deuxième feuille
SaveDoublon (DoublonNo)
End If
End If
Next j
LogFile.writeLine Date & " " & DateTime.Time
LogFile.Close
End Sub
Function SaveDoublon(Number As Long)
Worksheets(2).Select
Cells(DoublonLogNo, 10).Value = Number
DoublonLogNo = DoublonLogNo + 1
Worksheets(3).Select
End Function
Function traitement_doublon(numDoublon As Long) As Long
Dim rngSelect As Range
Range(Cells(2, 1), Cells(2, 1)).Select
Selection.AutoFilter Field:=16, Criteria1:=numDoublon
Selection.AutoFilter Field:=12, Criteria1:="X"
traitement_doublon = CountVisibleRows(2) ' Nombre de ligne des titres
End Function
Private Function ColorVisibleRows(HdgRowCnt As Integer) As Boolean 'le booleen passe à true si une des lignes nous concerne
Dim rTable As Range
Dim rCell As Range
Set rTable = ActiveSheet.UsedRange
For Each rCell In rTable.Resize(, 16).SpecialCells(xlCellTypeVisible)
If rCell.Row > HdgRowCnt Then
rCell.Interior.Color = RGB(255, 0, 0)
End If
If Cells(rCell.Row, 6) Like "*" & "CCSR/RTPL" & "*" Then
ColorVisibleRows = True
End If
Next rCell
End Function
Private Function CountVisibleRows(HdgRowCnt As Integer) As Integer
'******************************************************************************
'* - Count the number of visible data rows on the Active Sheet.
'* - Parameters:
'* HdgRowCnt = the number of heading rows on the sheet
'* - Output:
'* The result (the number of visible rows) is passed back to the calling
'* statement in the function name.
'******************************************************************************
Dim rTable As Range
Dim rCell As Range
Dim visibleRows As Integer
Set rTable = ActiveSheet.UsedRange
For Each rCell In rTable.Resize(, 1).SpecialCells(xlCellTypeVisible)
visibleRows = visibleRows + 1
Next rCell
CountVisibleRows = visibleRows - HdgRowCnt
End Function
Function AttachedDoc1(testedString As String) As Boolean
Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "_[A-B][0-9]{1,3}$"
AttachedDoc1 = reg.test(testedString)
End Function
Function AttachedDoc2(testedString As String) As Boolean
Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "[A-B][0-9]{1,3}(-rework|-Rework|-REWORK|).(pdf|PDF|zip|ZIP|xls|XLS|doc|DOC|mpp|MPP|log|LOG|dat|DAT)$"
AttachedDoc2 = reg.test(testedString)
End Function
Function DocNoOnly(testedString As String) As Boolean
'Expression régulière
Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "([a-zA-Z0-9]{1,5})(_)([a-zA-Z0-9]{1,5})"
DocNoOnly = reg.test(testedString)
End Function
Function FatherDocNoOnly(testedString As String) As String
'Fonction retournant les caractère situés à gauche du "_"
FatherDocNoOnly = Left(testedString, InStr(testedString, "_") - 1)
End Function |
Partager