Sub FormulesClé()
Dim dernligne As Long
Dim DernCol As Integer
DernCol = Sheets("Data").Cells(1, Cells.Columns.Count).End(xlToLeft).Column ' on cherche la dernière colonne de la feuille data
dernligne = Range("A" & Rows.Count).End(xlUp).Row 'on cherche le numéro de la dernière ligne de la colonne A
Dim derncolletter, jourphotocolletter As Variant
derncolletter = ToColletter(DernCol) 'récupération de la lettre de la dernière colonne grâce à la fonction tocolletter
Dim Dic As Object
Dim clecolletter As Variant
Dim searchBase, searchCentre, searchZone, searchCle, searchJourPhoto, searchDateinitiale As String
Dim Cl, acell, bcell, ccell, dcell, ecell As Range
searchBase = "******" 'on déclare les variables de recherche
searchCentre = "******" '
searchZone = "******" '
searchCle = "Clé" '
searchJourPhoto = "Jour Photo" '
searchDateinitiale = "Date Cible***" '
'après on paramètre les champs de recherche:
Set CelluleCle = Worksheets("Data").Rows(1).Find(What:=searchCle, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set acell = Worksheets("Data").Rows(1).Find(What:=searchBase, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set bcell = Worksheets("Data").Rows(1).Find(What:=searchCentre, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set ccell = Worksheets("Data").Rows(1).Find(What:=searchZone, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set dcell = Worksheets("Data").Rows(1).Find(What:=searchJourPhoto, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set ecell = Worksheets("Data").Rows(1).Find(What:=searchDateinitiale, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
clecolletter = ToColletter(CelluleCle.Column) ' identification de la lettre de colonne "Clé"
Dim srchRn As Range, c As Range
Set srchRn = Range(clecolletter & "2:" & clecolletter & dernligne) 'recherche dans la colonne "Clé"
For Each c In srchRn
If IsEmpty(c) Then
'ici pour faire la clé on concatène et le numéro de colonne de l'offset est obtenu grâce à la différence des numéros de colonne
c.Value = c.Offset(0, acell.Column - c.Column).Value & c.Offset(0, bcell.Column - c.Column).Value & c.Offset(0, ccell.Column - c.Column).Value
End If
Next
''''recherche les communes en créant un dictionnaire de FullCle''''''''''''
Set Dic = CreateObject("scripting.dictionary") 'nom du dico
With Sheets("FullCle") 'avec la feuille FullCle
For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp)) ' pour chacune des cellules de la colonne A
Dic(Cl.Value) = Cl.Offset(, 5).Value 'clé du dico = colonne A et item = valeur colonne en décalé de 5 sur la droite
Next Cl ' prochaine cellule dans la même colonne
End With
With Sheets("Data") ' avec la feuille Data maintenant
For Each Cl In .Range(clecolletter & "2", .Range(clecolletter & Rows.Count).End(xlUp)) 'le champ de recherche est positionné sur la colonne "clé"
If Dic.exists(Cl.Value) Then Cl.Offset(, 1).Value = Dic(Cl.Value) 'si la valeur de la clé Data est dans le dico alors la case de droite obtiendra la valeur item du dico soit ici la commune
Next Cl
End With
With Sheets("Data") 'même opération mais cette fois ci avec les 5 premoiers caractères de gauche
For Each Cl In .Range(clecolletter & "2", .Range(clecolletter & Rows.Count).End(xlUp))
If Dic.exists(Left(Cl.Value, 5)) Then Cl.Offset(, 1).Value = Dic(Left(Cl.Value, 5))
Next Cl
End With
Set Dic = Nothing 'reset du Dico
''''''''''''''''''''''''''''recherche les CA''''''''''''''''''''''''''''
Set Dic = CreateObject("scripting.dictionary")
With Sheets("FullCle")
For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
Dic(Cl.Value) = Cl.Offset(, 6).Value
Next Cl
End With
With Sheets("Data")
For Each Cl In .Range(clecolletter & "2", .Range(clecolletter & Rows.Count).End(xlUp))
If Dic.exists(Cl.Value) Then Cl.Offset(, 2).Value = Dic(Cl.Value)
Next Cl
End With
With Sheets("Data")
For Each Cl In .Range(clecolletter & "2", .Range(clecolletter & Rows.Count).End(xlUp))
If Dic.exists(Left(Cl.Value, 5)) Then Cl.Offset(, 2).Value = Dic(Left(Cl.Value, 5))
Next Cl
End With
Set Dic = Nothing
'''''''''''''''''''recherche zone renfort''''''''''''''''''''
Set Dic = CreateObject("scripting.dictionary")
With Sheets("FullCle")
For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
Dic(Cl.Value) = Cl.Offset(, 7).Value
Next Cl
End With
With Sheets("Data")
Application.ScreenUpdating = False
For Each Cl In .Range(clecolletter & "2", .Range(clecolletter & Rows.Count).End(xlUp))
If Dic.exists(Cl.Value) Then Cl.Offset(, 3).Value = Dic(Cl.Value)
Next Cl
End With
With Sheets("Data")
For Each Cl In .Range(clecolletter & "2", .Range(clecolletter & Rows.Count).End(xlUp))
If Dic.exists(Left(Cl.Value, 5)) Then Cl.Offset(, 3).Value = Dic(Left(Cl.Value, 5))
Next Cl
End With
Set Dic = Nothing
''''''''''''''''''''''''''''''calcul du retard'''''''''''''''''''''''
jourphotocolletter = ToColletter(dcell.Column) 'localisation de la colonne jourphoto
With Application
.ScreenUpdating = False
For Each c In .Range(jourphotocolletter & "2", .Range(jourphotocolletter & Rows.Count).End(xlUp))
On Error Resume Next
If DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) > 90 Then
c.Offset(, DernCol - dcell.Column).Value = "+ 90 jours"
ElseIf DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) < 8 Then
c.Offset(, DernCol - dcell.Column).Value = "- 7 Jours"
ElseIf DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) > 7 And DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) < 15 Then
c.Offset(, DernCol - dcell.Column).Value = "1 à 14 Jours"
ElseIf DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) > 14 And DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) < 31 Then
c.Offset(, DernCol - dcell.Column).Value = "15 à 30 Jours"
ElseIf DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) > 30 And DateDiff("d", c.Offset(, ecell.Column - dcell.Column), c) < 91 Then
c.Offset(, DernCol - dcell.Column).Value = "1 à 3 Mois"
Else
c.Offset(, DernCol - dcell.Column).Value = "à venir"
End If
Next
End With
Sheets("Dashboard").Activate
End Sub