If UBound(moy) > 0 Then
If moy(0) <> "" Then
non, on ne peut faire des différences sur les heures comme on les fait en base 10
If UBound(cols) = 0 Then Exit Function
Public Function verif(cols, colonne_groupes, a_partir_ligne, descr As String) As Boolean Dim derLig As Long Dim plage As Range Dim elmt As Variant verif = True If UBound(cols) = 0 Then Exit Function
Groupe 0 : 16,45 - 16,05 = 0,40 Min (Ok avec ton code)
Groupe 1 : 16,59 - 16,45 = 0,14 Min (ou est le loup ???)
If UBound(cols) = 0 Then Exit Function
If cols(0) = "" Then Exit Function
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionJe suis d'accord avec toi sur l'exemple que tu donnes. C'est justement ce que j’essayais de te dire dans mon post ou j’abordais cette problématique. On ne peut pas TOUT faire avec les heures. Pour éviter ce genres d'erreurs, c'est pour quoi j'ai volontairement décidé d'utiliser la plage [16H00;16H59]. Ainsi la fonction différence est très bien adaptée.
Un essai peut durer 10 min, 1 heure, 1 jour etc...
Public Sub epurer(ByVal col As String, ByVal ligne As Integer, ByVal nb As Integer, ByVal col_tremplin As Integer, ByVal dif, ByVal deb As Single) Dim plage As Range, plage_a_supp As Range, ahah As Single Dim n As Long, i As Long, j As Long, msg As String n = Range(col & Rows.Count).End(xlUp).Row msg = "" For i = ligne + 1 To n + 1 If Range(col & i).Value = Range(col & i - 1).Value Then If plage Is Nothing Then Set plage = Union(Range(col & i - 1), Range(col & i)) Else Set plage = Union(plage, Range(col & i)) End If ElseIf Not plage Is Nothing Then Dim R As Range If dif(0) <> "" Then For Each R In plage.Rows ahah = Cells(plage.Row + plage.Rows.Count - 1, 2).Value Cells(R.Row, dif(0)).Value = ahah - deb Next End If If dif(0) <> "" Then deb = ahah If plage.Rows.Count > (nb * 2) + 1 Then For j = 1 To nb If plage_a_supp Is Nothing Then Set plage_a_supp = Union(plage(1, 1), plage(plage.Rows.Count, 1)) Else Set plage_a_supp = Union(plage_a_supp, plage(j, 1), plage(plage.Rows.Count + 1 - j, 1)) End If Next Set plage = Range(col & i) If Range(col & i).Value = "" Then Exit For Else msg = msg & " - " & plage(1, 1).Value Set plage = Nothing If Range(col & i).Value = "" Then Exit For End If Else msg = msg & " - " & Cells(i - 1, 1).Value End If Next If Not plage_a_supp Is Nothing Then plage_a_supp.Rows.EntireRow.Delete End If If msg <> "" Then MsgBox "les groupes suivants, d'un nombre non suffisant, " & _ "n'ont pas été traités " & vbCrLf & Mid(msg, 3) End If End Sub
Procède à tes vérifications.
Si confirmé ===>> il me faudra :
1) faire un petit ménage (nous n'avons plus de colonne tremplin)
12) passer en "souple" une ligne qui est encore en "dur" (un 2 qui fige sur la colonne B au lieu d'un x correspondant à la colonne des différences). Mais cela est super-facile et déjà fait, entre-temps.
Option Explicit Sub DIFFERENCE_SUPPRESSION_MOYENNE() '''''' tout ton début de reconstruction '''''' '''''' Dim colonne_groupes As String, a_partir_ligne As Integer, ecreter_de_combien As Long, Minimum_devant_rester As Integer Dim depart As Single Dim col_groupes As Variant, colonnes_moyennes As Variant, colonnes_diff As Variant ' ==============================c'est ici que tu définis la feuille des données ========================================== feuille_donnees = "MACRO" ' <<<<<========= le nom exact de la feuille contenant les groupes à traiter ''c'est ici, que tu définis les autres paramètres ========================================= colonne_groupes = "A" '<<<<<<<======== ici : la colonne des groupes a_partir_ligne = 2 '<<<<<<============ ici : la ligne où commencent les groupes ecreter_de_combien = 2 '<<<<======= ici : de combien écréter en haut et en bas de chaque groupe Minimum_devant_rester = 1 '<<<<======= ici : écrêtement si ce minimum après écrêtement sinon non depart = 0 ' <<<<<===== ligne rajoutée ici (et la valeur de ton choix, sous forme avec . comme séparateur décimal si utilisé, hein ...) colonnes_moyennes = Array("C") '<<<<=== ici : énumération des colonnes où faire la moyenne ("") si aucune colonnes_diff = Array("B") ' <<<<<===== ici : la SEULE colonne où faire ta "différences" ("") si aucune col_groupes = Array(colonne_groupes) ' ---->> ça, c'est pour le code. Ne t'en préoccupe pas. '========================================================================================================================== ' ________________________________________ les gardes-fou, maintenant ________________________________________________ If ActiveSheet.Name <> feuille_donnees Then MsgBox "cette opération ne doit être lancée que si la feuille " & feuille_donnees & " est la feuille active" Exit Sub End If If verif(col_groupes, colonne_groupes, a_partir_ligne, "colonne des groupes") = False Then Exit Sub If verif(colonnes_moyennes, colonne_groupes, a_partir_ligne, "colonne à moyenne") = False Then Exit Sub If verif(colonnes_diff, colonne_groupes, a_partir_ligne, "colonne à différence") = False Then Exit Sub '______________________________________________________________________________________________________________________ ' ------------------------------ si l'exécution atteint ce point, c'est que tout est cohérent ------------------------- ' ------------------------------ on y va donc ------------------------------------------------------------------------- ' ---------------------------------------------"écrétant" d'abord------------------------------------------------------ epurer colonne_groupes, a_partir_ligne, ecreter_de_combien, Minimum_devant_rester, colonnes_diff, depart ' ----------------------------------------puis en traitant le reste (écart(s) et moyenne(s) --------------------------- MsgBox "On vient d'épurer" on_amenage colonne_groupes, a_partir_ligne, colonnes_moyennes, colonnes_diff ActiveSheet.Buttons.Add(440, 15, 120, 45).Select Selection.OnAction = "BASE_DE_DONNEES_FINALE" Selection.Characters.Text = "BASE DE DONNEES INNITIALE" ActiveSheet.Buttons.Add(600, 15, 120, 45).Select Selection.OnAction = "DIFFERENCE_SUPPRESSION_MOYENNE" Selection.Characters.Text = "BASE DE DONNEES FINALE" End Sub Public Sub epurer(ByVal col As String, ByVal ligne As Integer, ByVal nb As Integer, ByVal mini As Integer, ByVal dif, ByVal deb As Single) Dim plage As Range, plage_a_supp As Range, ahah As Single Dim n As Long, i As Long, j As Long, msg As String n = Range(col & Rows.Count).End(xlUp).Row msg = "" For i = ligne + 1 To n + 1 If Range(col & i).Value = Range(col & i - 1).Value Then If plage Is Nothing Then Set plage = Union(Range(col & i - 1), Range(col & i)) Else Set plage = Union(plage, Range(col & i)) End If ElseIf Not plage Is Nothing Then Dim R As Range If dif(0) <> "" Then For Each R In plage.Rows ahah = Cells(plage.Row + plage.Rows.Count - 1, dif(0)).Value Cells(R.Row, dif(0)).Value = ahah - deb Next End If If dif(0) <> "" Then deb = ahah If plage.Rows.Count > (nb * 2) + mini - 1 Then For j = 1 To nb If plage_a_supp Is Nothing Then Set plage_a_supp = Union(plage(1, 1), plage(plage.Rows.Count, 1)) Else Set plage_a_supp = Union(plage_a_supp, plage(j, 1), plage(plage.Rows.Count + 1 - j, 1)) End If Next Set plage = Range(col & i) If Range(col & i).Value = "" Then Exit For Else msg = msg & " - " & plage(1, 1).Value Set plage = Nothing If Range(col & i).Value = "" Then Exit For End If Else msg = msg & " - " & Cells(i - 1, 1).Value End If Next If Not plage_a_supp Is Nothing Then plage_a_supp.Rows.EntireRow.Delete End If If msg <> "" Then MsgBox "les groupes suivants, d'un nombre non suffisant, " & _ "n'ont pas été traités " & vbCrLf & Mid(msg, 3) End If End Sub Public Sub on_amenage(ByVal col As String, ByVal ld As Integer, ByVal moy, ByVal dif) Dim deb As Long, n As Long, i As Long, combien As Long, k As Long, j As Long Dim plage As Range, plage_a_supp As Range Dim elmt deb = Range(col & ":" & col).Column n = Range(col & Rows.Count).End(xlUp).Row For i = ld + 1 To n + 1 If Range(col & i).Value = Range(col & i - 1).Value Then If plage Is Nothing Then Set plage = Union(Range(col & i - 1), Range(col & i)) Else Set plage = Union(plage, Range(col & i)) End If Else If Not plage Is Nothing Then combien = plage.Rows.Count 'MsgBox plage.Address If combien > 1 Then If moy(0) <> "" Then For Each elmt In moy 'MsgBox k k = Range(elmt & ":" & elmt).Column - deb + 1 plage.Cells(1, k) = WorksheetFunction.Average(plage.Columns(k)) 'MsgBox plage.Columns(k).Address Next End If For j = 2 To combien If plage_a_supp Is Nothing Then Set plage_a_supp = plage(j, 1) Else Set plage_a_supp = Union(plage_a_supp, plage(j, 1)) End If Next End If Set plage = Nothing End If End If Next If Not plage_a_supp Is Nothing Then plage_a_supp.Rows.EntireRow.Delete End If End Sub Public Function verif(cols, colonne_groupes, a_partir_ligne, descr As String) As Boolean Dim derLig As Long Dim plage As Range Dim elmt As Variant verif = True If cols(0) = "" Then Exit Function derLig = Range(colonne_groupes & Rows.Count).End(xlUp).Row For Each elmt In cols Set plage = Nothing On Error Resume Next Set plage = Range(elmt & a_partir_ligne & ":" & elmt & derLig).SpecialCells(xlCellTypeBlanks) If Not plage Is Nothing Then MsgBox "la " & descr & " " & elmt & " contient une cellule vide - Corrigez puis relancez, s'il vous plait !" verif = False On Error GoTo 0 End If Next End Function