Bonjour à tous,
j'ai écrit ce code VBA (avec plusieurs aides et tutos) mais il bloque à la ligne (TAT = RechercherTAT(Tableclient, client, entite, typeOffre) avec comme erreur l'erreur 438 (propriété non prise en charge).
J'aimerais savoir si quelqu'un peut m'aider et si il y auraient d'autres erreurs plus loin dans le code.
Si il y a besoin je peux expliquer en détail ce que je veux que le code exécute (un peu compliqué mais ça le fait).
Merci d'avance de votre aide et bonne journée !
Sub PredireDateCommande()
Dim wsSuivi As Worksheet
Dim wsTAT As Worksheet
Dim lastRow As Long
Dim i As Long
Dim client As String
Dim entite As String
Dim typeOffre As String
Dim dateEnvoi As Date
Dim TAT As Long
Dim dateCommande As Date
Dim currentDate As Date
Dim dateFinValidite As Date
Dim dateCommandeEstimeeCol As String
Dim Suivitable As ListObject
Set Suivitable = ThisWorkbook.Sheets("Tableausuivi").ListObjects("SuiviTable")
Dim Tableclient As ListObject
Set Tableclient = ThisWorkbook.Sheets("TAT MOYEN CLIENT").ListObjects("TableClient")
' Trouver les colonnes avec les noms de données
Dim clientCol As String
Dim entiteCol As String
Dim typeOffreCol As String
Dim dateEnvoiCol As String
clientCol = FindColumn(Suivitable, "Client / DO")
entiteCol = FindColumn(Suivitable, "Entité")
typeOffreCol = FindColumn(Suivitable, "Type offre")
dateEnvoiCol = FindColumn(Suivitable, "Dt envoi Offre")
dateCommandeEstimeeCol = FindColumn(Suivitable, "Date commande estimé sur REX")
' Trouver la dernière ligne avec des données dans Suivitable
lastRow = Suivitable.DataBodyRange.Rows.Count
' Boucler à travers les lignes de Suivitable
For i = 2 To lastRow ' Commencer à la deuxième ligne si les en-têtes sont en première ligne
' Lire les données de Suivitable
client = Suivitable.DataBodyRange.Cells(i, clientCol).Value
entite = Suivitable.DataBodyRange.Cells(i, entiteCol).Value
typeOffre = Suivitable.DataBodyRange.Cells(i, typeOffreCol).Value
dateEnvoi = Suivitable.DataBodyRange.Cells(i, dateEnvoiCol).Value
dateCommande = Suivitable.DataBodyRange.Cells(i, dateCommandeEstimeeCol).Value ' Lecture de la colonne existante
currentDate = Date
dateFinValidite = Suivitable.Cells(i, FindColumn(Suivitable, "Date fin validité")).Value
' Recherche du TAT correspondant dans la feuille TAT
TAT = RechercherTAT(Tableclient, client, entite, typeOffre)
Do While dateEnvoi + TAT <= currentDate And dateEnvoi + TAT <= dateFinValidite
TAT = RechercherTAT(Tableclient, client, entite, typeOffre)
dateEnvoi = dateEnvoi + TAT
Loop
' Mettre à jour la date de commande estimée dans Suivitable
Suivitable.Cells(i, dateCommandeEstimeeCol).Value = dateEnvoi ' Écriture dans la colonne existante
Next i
End Sub
Function FindColumn(ws As ListObject, columnName As String) As String
Dim headerCell As Range
For Each headerCell In ws.HeaderRowRange.Cells
If headerCell.Value = columnName Then
FindColumn = Split(headerCell.Address, "$")(1)
Exit Function
End If
Next headerCell
FindColumn = ""
End Function
Function RechercherTAT(wsTAT As ListObject, client As String, entite As String, typeOffre As String) As Integer
Dim lastRow As Long
Dim i As Long
' Trouver la dernière ligne avec des données dans TAT
lastRow = wsTAT.Cells(wsTAT.Rows.Count, "A").End(xlUp).Row
' Boucler à travers les lignes de TAT
For i = 2 To lastRow ' Commencer à la deuxième ligne si les en-têtes sont en première ligne
' Vérifier les conditions
If wsTAT.Cells(i, "A").Value = client And wsTAT.Cells(i, "B").Value = entite And wsTAT.Cells(i, "C").Value = typeOffre Then
RechercherTAT = wsTAT.Cells(i, "G").Value
Exit Function
End If
Next i
' Retourner 0 si aucune correspondance n'est trouvée
RechercherTAT = 0
End Function
Partager