This will update all Min/Max whenever Exercise sheet is selected from other sheet.
That means all the changes you manually made to the Min/Max in Exercise sheet will be replaced with actual Min/Max from all the Week xx sheet(s).
Replace current code with
Private Sub Worksheet_Activate()
Update Range("a2", Range("a" & Rows.Count).End(xlUp))
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 Then Exit Sub
If Not Intersect(Target, Columns(1)) Is Nothing Then
Target.Range("b1:c1").ClearContents
Update Target
End If
If Not Intersect(Target, Columns("b:c")) Is Nothing Then
If IsNumeric(Target) Then FixData Target
End If
End Sub
Sub Update(rng As Range)
Dim r As Range, x(2), i As Long, s As String, ws As Worksheet
Application.EnableEvents = False
rng.Offset(, 1).Resize(, 2).ClearContents
For Each ws In Worksheets
If LCase$(ws.Name) Like "week *" Then
x(0) = ws.Range("a" & Rows.Count).End(xlUp).Row
For Each r In rng
x(1) = Filter(ws.Evaluate("transpose(if(a2:a" & x(0) & "=""" & r & """,row(2:" & x(0) & ")))"), False, 0)
If UBound(x(1)) > -1 Then
For i = 0 To UBound(x(1))
s = "b" & x(1)(i) & ":k" & x(1)(i)
x(2) = ws.Evaluate("sumproduct((" & s & "<>"""")*(isnumber(" & s & "+0))*(mod(column(" & s & "),2)=0))")
If x(2) > 0 Then
x(2) = ws.Evaluate("min(if((" & s & "<>"""")*(isnumber(" & s & "+0))*(mod(column(" & s & "),2)=0)*(isnumber(" & s & "+0))," & s & "+0))")
If r(, 2) = "" Then r(, 2) = x(2)
If r(, 2) > x(2) Then r(, 2) = x(2)
x(2) = ws.Evaluate("max(if((" & s & "<>"""")*(isnumber(" & s & "+0))*(mod(column(" & s & "),2)=0)*(isnumber(" & s & "+0))," & s & "+0))")
If r(, 3) = "" Then r(, 3) = x(2)
If r(, 3) < x(2) Then r(, 3) = x(2)
End If
Next
End If
Next
End If
Next
Application.EnableEvents = True
End Sub
Sub FixData(r As Range)
Dim x, i As Long, myMin As Double, myMax As Double, temp As Double
Application.EnableEvents = False
x = Filter(Evaluate("transpose(if(a2:a50000=a" & r.Row & ",row(2:50000)))"), False, 0)
If r.Column = 2 Then
myMin = r
myMax = Evaluate("max(if((a2:a50000=a" & r.Row & ")*(c2:c50000<>""""),c2:c50000))")
If myMin > myMax Then myMax = myMin
Else
myMax = r
myMin = Evaluate("min(if((a2:a50000=a" & r.Row & ")*(b2:b50000<>""""),b2:b50000))")
If myMin > myMax Then myMin = myMax
End If
For i = 0 To UBound(x)
Cells(x(i), 2).Resize(, 2) = Array(myMin, myMax)
Next
Application.EnableEvents = True
End Sub
Bookmarks