Sub Button1_Click()
Dim WSSource As Worksheet, WSCible As Worksheet 'indique que Feuil1 et Feuil2 sont des Feuilles
Dim CellSource As Range, PlageSource As Range 'indique que CellSource et PlageSource sont des sélections de cellules
Dim CellCible As Range, PlageCible As Range 'indique que CellCible et PlageCible sont des sélections de cellules
Dim MyDate As Date 'indique que MyDate est au format Date
Dim c As Byte, x As Byte 'indique que C et X sont au format 8 byte donc chiffre de 0 à 255
Dim NotTreated As String 'indique que NotTreated est au format texte
Set WSSource = ThisWorkbook.Worksheets("Feuil1") 'on appelle la Feuill1
Set WSCible = ThisWorkbook.Worksheets("Feuil2") 'on appelle la Feuill2
Set PlageSource = WSSource.Range("B40:B" & WSSource.Range("B1000").End(xlUp).Row) 'On sélectionne la plage de cellules à partir de B40 et du coup la suite de la colonne B à partir de B40. Et on remonte à partir de B1000 en listant les lignes ?
Set PlageCible = WSCible.Range("C8:C" & WSCible.Range("C100").End(xlUp).Row) 'la même qu'au dessus !
WSCible.Range("D8:R27").ClearContents 'Supprime la plage de cellule avant de commencer
For Each CellSource In PlageSource 'For Each indique une boucle des différents cellsource dans la plage B40:B
For Each CellCible In PlageCible 'la même sur la place C8:C
If CellSource.Text = CellCible.Text Then 'On verifie que la plage source = cible au format texte ?
If DatePart("h", CellSource.Offset(0, -1)) >= 13 Then 'Si dans l'intervalle de 0 à 23h dans la colonne Date de fin (A) on est supérieur à 13h puis
Select Case Weekday(CellSource.Offset(0, -1)) 'on sélectionne les jours de la semaine dans la colonne A
Case 6 'Friday
MyDate = Format(DateAdd("d", 3, CellSource.Offset(0, -1)), "dd/mm/yyyy") 'Pourquoi Case 6 ? Tableau commençant par C8 + 5 jours donc total 6 colonnes pour arriver à vendredi ? MyDate = Format jour/mois/année. On ajoute 3 pour le faire venir au lundi
Case 7 'Saturday
MyDate = Format(DateAdd("d", 2, CellSource.Offset(0, -1)), "dd/mm/yyyy") 'la même +2
Case 1 'Sunday
MyDate = Format(DateAdd("d", 1, CellSource.Offset(0, -1)), "dd/mm/yyyy") 'Case 1 ? la même +1 pour faire venir au lundi
Case Else
MyDate = Format(DateAdd("d", 1, CellSource.Offset(0, -1)), "dd/mm/yyyy") 'Sinon on ajoute automatiquement +1
End Select
Else 'Sinon si inférieur à 13h, on recommence sauf le vendredi qui sera mis au vendredi
Select Case Weekday(CellSource.Offset(0, -1))
Case 7 'Saturday
MyDate = Format(DateAdd("d", 2, CellSource.Offset(0, -1)), "dd/mm/yyyy")
Case 1 'Sunday
MyDate = Format(DateAdd("d", 1, CellSource.Offset(0, -1)), "dd/mm/yyyy")
Case Else
MyDate = Format(DateAdd("d", 0, CellSource.Offset(0, -1)), "dd/mm/yyyy")
End Select
End If
For c = 1 To 15 'on cible le nombre de ligne/boucle
If MyDate = WSCible.Cells(6, c + 3) Then 'A partir de là, j'ai besoin d'explication !!!
For x = 1 To 3
If WSCible.Cells(7, c + 3) = WSSource.Cells(1, x + 2) Then
If CellCible.Offset(0, c) = "" And CellCible.Offset(0, c + 1) = "" And CellCible.Offset(0, c + 2) = "" Then
CellCible.Offset(0, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(1, c) = "" And CellCible.Offset(1, c + 1) = "" And CellCible.Offset(1, c + 2) = "" Then
CellCible.Offset(1, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(2, c) = "" And CellCible.Offset(2, c + 1) = "" And CellCible.Offset(2, c + 2) = "" Then
CellCible.Offset(2, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(3, c) = "" And CellCible.Offset(3, c + 1) = "" And CellCible.Offset(3, c + 2) = "" Then
CellCible.Offset(3, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(4, c) = "" And CellCible.Offset(4, c + 1) = "" And CellCible.Offset(4, c + 2) = "" Then
CellCible.Offset(4, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(5, c) = "" And CellCible.Offset(5, c + 1) = "" And CellCible.Offset(5, c + 2) = "" Then
CellCible.Offset(5, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(6, c) = "" And CellCible.Offset(6, c + 1) = "" And CellCible.Offset(6, c + 2) = "" Then
CellCible.Offset(6, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(7, c) = "" And CellCible.Offset(7, c + 1) = "" And CellCible.Offset(7, c + 2) = "" Then
CellCible.Offset(7, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(8, c) = "" And CellCible.Offset(8, c + 1) = "" And CellCible.Offset(8, c + 2) = "" Then
CellCible.Offset(8, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(9, c) = "" And CellCible.Offset(9, c + 1) = "" And CellCible.Offset(9, c + 2) = "" Then
CellCible.Offset(9, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(10, c) = "" And CellCible.Offset(10, c + 1) = "" And CellCible.Offset(10, c + 2) = "" Then
CellCible.Offset(10, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(11, c) = "" And CellCible.Offset(11, c + 1) = "" And CellCible.Offset(11, c + 2) = "" Then
CellCible.Offset(11, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(12, c) = "" And CellCible.Offset(12, c + 1) = "" And CellCible.Offset(12, c + 2) = "" Then
CellCible.Offset(12, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(13, c) = "" And CellCible.Offset(13, c + 1) = "" And CellCible.Offset(13, c + 2) = "" Then
CellCible.Offset(13, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(14, c) = "" And CellCible.Offset(14, c + 1) = "" And CellCible.Offset(14, c + 2) = "" Then
CellCible.Offset(14, c) = CellSource.Offset(0, x)
ElseIf CellCible.Offset(15, c) = "" And CellCible.Offset(15, c + 1) = "" And CellCible.Offset(15, c + 2) = "" Then
CellCible.Offset(15, c) = CellSource.Offset(0, x)
Else
NotTreated = NotTreated & " -- " & CellSource.Offset(0, x) & vbCrLf 'sinon pas traité &"Texte"& info des plages non traité & à la ligne
End If
End If
Next x '?
End If
Next c '?
Exit For
End If
Next CellCible '?
Next CellSource '?
If NotTreated <> "" Then 'si ce qui n'a pas été traité <> car nombre de ligne dépassé puis afficher la box
MsgBox "Enregistrement(s) dépassant(s) la capacité" & vbCrLf & NotTreated
End If
End Sub