Code to fill the highest & lowest value from a range in 2 ranges

hsandeep

Well-known Member
Joined
Dec 6, 2008
Messages
1,226
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
Range C2:C17 updates using formula & changes its value very fast.

In F2:F17 the highest value occurred in the cells C2:C17 is filled in ‘RESPECTIVE/CORRESPONDING’ cells of F2:F17 when A1=1 else ‘nothing is filled’.
In G2:G17 the lowest value occurred in the cells C2:C17 is filled in ‘RESPECTIVE/CORRESPONDING’ cells of G2:G17 when A1=1 else ‘nothing is filled’.

For this I am using the below code successfully till large extent (although it is slow in execution)

Rich (BB code):
Private Sub Worksheet_Calculate()
'Code for Sheet H51
'Code checks/updates HIGHEST value occurred in C2:C16 & fills in F2:F16; C17 & fills in F17 when A1=1
'Code checks/updates LOWEST value occurred in C2:C3 & fills in G2:G16; C17 & fills in G17 when A1=1
Dim cell As Range
'Exit if A1 is not equal to 1
If Range("A1") <> 1 Then Exit Sub

'Loop through range of values that is being updated in C2:C16 & C17
For Each cell In Range("C2:C17")

'Check/update Maximum HIGHEST Value occurred in C2:C16 and FILL it in F2:F16; C17 in F17
If (Len(cell.Offset(0, 3)) > 0) And (IsNumeric(cell.Offset(0, 3))) Then
If cell > cell.Offset(0, 3) Then cell.Offset(0, 3) = cell
Else
cell.Offset(0, 3) = cell
End If

'Check/update Minimum LOWEST Value occurred in C2:C16 and FILL it in G2:G16; C17 in G17
If (Len(cell.Offset(0, 4)) > 0) And (IsNumeric(cell.Offset(0, 4))) Then
If cell < cell.Offset(0, 4) Then cell.Offset(0, 4) = cell
Else
cell.Offset(0, 4) = cell
End If
Next cell
End Sub

Book2.xlsx
ABCDEFG
10ValueValue HIGHESTValue LOWEST
21140.10
31042.15
4942.35
5850.00
6759.95
7665.55
8579.45
9498.25
10422.00
11348.00
12283.85
13227.00
14177.95
15133.60
16100.65
1743233.90
H51


I want a new code to ADD the below action.
Range M2:M17 updates using formula & changes its value very fast. (in the same worksheet)

In P2:P17 the highest value occurred in the cells M2:M17 SHOULD BE FILLED in ‘RESPECTIVE/CORRESPONDING’ cells of P2:P17 when K1=1 else ‘nothing should be filled’.
In Q2:Q17 the lowest value occurred in the cells M2:M17 SHOULD BE FILLED in ‘RESPECTIVE/CORRESPONDING’ cells of Q2:Q17 when K1=1 else ‘nothing should be filled’.
Book2.xlsx
ABCDEFGHIJKLMNOPQ
10ValueValue HIGHESTValue LOWEST0ValueValue HIGHESTValue LOWEST
21140.101140.10
31042.151042.15
4942.35942.35
5850.00850.00
6759.95759.95
7665.55665.55
8579.45579.45
9498.25498.25
10422.00422.00
11348.00348.00
12283.85283.85
13227.00227.00
14177.95177.95
15133.60133.60
16100.65100.65
1743233.9043233.90
H51


Thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Please consider this code:
VBA Code:
Private Sub Worksheet_Calculate()
  Dim myRangeC As Variant, myRangeCHtoL As Variant, myRangeCLtoH As Variant, myRangeM As Variant, myRangeMHtoL As Variant, myRangeMLtoH As Variant
  Dim i As Long
  myRangeC = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)
  myRangeM = Range("M2:M" & Range("M" & Rows.Count).End(xlUp).Row)
  ReDim myRangeCHtoL(1 To UBound(myRangeC), 1 To 1)
  ReDim myRangeCLtoH(1 To UBound(myRangeC), 1 To 1)
  ReDim myRangeMHtoL(1 To UBound(myRangeM), 1 To 1)
  ReDim myRangeMLtoH(1 To UBound(myRangeM), 1 To 1)
  If Range("A1").Value = 1 Then
    For i = 1 To UBound(myRangeC, 1)
      myRangeCHtoL(i,1) = Application.Large(myRangeC, i)
      myRangeCLtoH(i,1) = Application.Small(myRangeC, i)
    Next
  End If
  If Range("K1").Value = 1 Then
    For i = 1 To UBound(myRangeM, 1)
      myRangeMHtoL(i,1) = Application.Large(myRangeM, i)
      myRangeMLtoH(i,1) = Application.Small(myRangeM, i)
    Next
  End If
  Application.ScreenUpdating = False
  Range("F2").Resize(UBound(myRangeCHtoL, 1), UBound(myRangeCHtoL, 2)) = myRangeCHtoL
  Range("G2").Resize(UBound(myRangeCLtoH, 1), UBound(myRangeCLtoH, 2)) = myRangeCLtoH
  Range("P2").Resize(UBound(myRangeMHtoL, 1), UBound(myRangeMHtoL, 2)) = myRangeMHtoL
  Range("Q2").Resize(UBound(myRangeMLtoH, 1), UBound(myRangeMLtoH, 2)) = myRangeMLtoH
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Even you can get the cell updates inside ifs:
VBA Code:
Private Sub Worksheet_Calculate()
  Dim myRangeC As Variant, myRangeCHtoL As Variant, myRangeCLtoH As Variant, myRangeM As Variant, myRangeMHtoL As Variant, myRangeMLtoH As Variant
  Dim i As Long

  myRangeC = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)
  myRangeM = Range("M2:M" & Range("M" & Rows.Count).End(xlUp).Row)
  ReDim myRangeCHtoL(1 To UBound(myRangeC), 1 To 1)
  ReDim myRangeCLtoH(1 To UBound(myRangeC), 1 To 1)
  ReDim myRangeMHtoL(1 To UBound(myRangeM), 1 To 1)
  ReDim myRangeMLtoH(1 To UBound(myRangeM), 1 To 1)

  Application.ScreenUpdating = False
  If Range("A1").Value = 1 Then
    For i = 1 To UBound(myRangeC, 1)
      myRangeCHtoL(i,1) = Application.Large(myRangeC, i)
      myRangeCLtoH(i,1) = Application.Small(myRangeC, i)
    Next
    Range("F2").Resize(UBound(myRangeCHtoL, 1), UBound(myRangeCHtoL, 2)) = myRangeCHtoL
    Range("G2").Resize(UBound(myRangeCLtoH, 1), UBound(myRangeCLtoH, 2)) = myRangeCLtoH
  End If
  If Range("K1").Value = 1 Then
    For i = 1 To UBound(myRangeM, 1)
      myRangeMHtoL(i,1) = Application.Large(myRangeM, i)
      myRangeMLtoH(i,1) = Application.Small(myRangeM, i)
    Next
    Range("P2").Resize(UBound(myRangeMHtoL, 1), UBound(myRangeMHtoL, 2)) = myRangeMHtoL
    Range("Q2").Resize(UBound(myRangeMLtoH, 1), UBound(myRangeMLtoH, 2)) = myRangeMLtoH
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Even you can get the cell updates inside ifs:
VBA Code:
Private Sub Worksheet_Calculate()
  Dim myRangeC As Variant, myRangeCHtoL As Variant, myRangeCLtoH As Variant, myRangeM As Variant, myRangeMHtoL As Variant, myRangeMLtoH As Variant
  Dim i As Long

  myRangeC = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)
  myRangeM = Range("M2:M" & Range("M" & Rows.Count).End(xlUp).Row)
  ReDim myRangeCHtoL(1 To UBound(myRangeC), 1 To 1)
  ReDim myRangeCLtoH(1 To UBound(myRangeC), 1 To 1)
  ReDim myRangeMHtoL(1 To UBound(myRangeM), 1 To 1)
  ReDim myRangeMLtoH(1 To UBound(myRangeM), 1 To 1)

  Application.ScreenUpdating = False
  If Range("A1").Value = 1 Then
    For i = 1 To UBound(myRangeC, 1)
      myRangeCHtoL(i,1) = Application.Large(myRangeC, i)
      myRangeCLtoH(i,1) = Application.Small(myRangeC, i)
    Next
    Range("F2").Resize(UBound(myRangeCHtoL, 1), UBound(myRangeCHtoL, 2)) = myRangeCHtoL
    Range("G2").Resize(UBound(myRangeCLtoH, 1), UBound(myRangeCLtoH, 2)) = myRangeCLtoH
  End If
  If Range("K1").Value = 1 Then
    For i = 1 To UBound(myRangeM, 1)
      myRangeMHtoL(i,1) = Application.Large(myRangeM, i)
      myRangeMLtoH(i,1) = Application.Small(myRangeM, i)
    Next
    Range("P2").Resize(UBound(myRangeMHtoL, 1), UBound(myRangeMHtoL, 2)) = myRangeMHtoL
    Range("Q2").Resize(UBound(myRangeMLtoH, 1), UBound(myRangeMLtoH, 2)) = myRangeMLtoH
  End If
  Application.ScreenUpdating = True
End Sub
@Flashbond
Many thanks for your interest shown & time & efforts given.
I think this code (thread #4) should run faster (but not tested till yet)

Actually, my total requirement is
There total 12 such ranges (10 more from the above 2 ranges) which are symmetrical in placement. The code’s action required is also ‘similar’
Example: (12th Range: )
Range DI2:DI17 updates using formula & changes its value very fast. (in the same worksheet)

In DL2:DL17 the highest value occurred in the cells DI2:DI17 SHOULD BE FILLED in ‘RESPECTIVE/CORRESPONDING’ cells of DL2:DL17 when DG1=1 else ‘nothing should be filled’.
In DM2:DM17 the lowest value occurred in the cells DI2:DI17 SHOULD BE FILLED in ‘RESPECTIVE/CORRESPONDING’ cells of DM2:DM17 when DG1=1 else ‘nothing should be filled’.

So I want to ADD 10 more ranges & run the code to give the desired results.
Main criteria is SPEED (code’s execution) & to avoid un-necessary loops.

Note: When A1 or its analogous like K1….till DG1 once they become 0 from 1 then they never become 1 again for that day. They remain 0.
Only 1 cell out of A1; K1; U1; AE1…..DG1 will be 1.

How to make the code FASTEST?
Book2.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCCCDCECFCGCHCICJCKCLCMCNCOCPCQCRCSCTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDJDKDLDM
10Value1Value HIGHESTValue LOWEST0Value2Value HIGHESTValue LOWEST0Value3Value HIGHESTValue LOWEST0Value4Value HIGHESTValue LOWEST0Value5Value HIGHESTValue LOWEST0Value6Value HIGHESTValue LOWEST0Value7Value HIGHESTValue LOWEST0Value8Value HIGHESTValue LOWEST0Value9Value HIGHESTValue LOWEST0Value10Value HIGHESTValue LOWEST0Value11Value HIGHESTValue LOWEST0Value12Value HIGHESTValue LOWEST
21140.101140.101140.101140.101140.101140.101140.101140.101140.101140.101140.101140.10
31042.151042.151042.151042.151042.151042.151042.151042.151042.151042.151042.151042.15
4942.35942.35942.35942.35942.35942.35942.35942.35942.35942.35942.35942.35
5850.00850.00850.00850.00850.00850.00850.00850.00850.00850.00850.00850.00
6759.95759.95759.95759.95759.95759.95759.95759.95759.95759.95759.95759.95
7665.55665.55665.55665.55665.55665.55665.55665.55665.55665.55665.55665.55
8579.45579.45579.45579.45579.45579.45579.45579.45579.45579.45579.45579.45
9498.25498.25498.25498.25498.25498.25498.25498.25498.25498.25498.25498.25
10422.00422.00422.00422.00422.00422.00422.00422.00422.00422.00422.00422.00
11348.00348.00348.00348.00348.00348.00348.00348.00348.00348.00348.00348.00
12283.85283.85283.85283.85283.85283.85283.85283.85283.85283.85283.85283.85
13227.00227.00227.00227.00227.00227.00227.00227.00227.00227.00227.00227.00
14177.95177.95177.95177.95177.95177.95177.95177.95177.95177.95177.95177.95
15133.60133.60133.60133.60133.60133.60133.60133.60133.60133.60133.60133.60
16100.65100.65100.65100.65100.65100.65100.65100.65100.65100.65100.65100.65
1743233.9043233.9043233.9043233.9043233.9043233.9043233.9043233.9043233.9043233.9043233.9043233.90
H51
 
Upvote 0
Then this should work:
VBA Code:
Private Sub Worksheet_Calculate()
  Dim myRange As Variant
  Dim c As Long, r As Long

  myRange = UsedRange
  With Application
  For c = 1 To UBound(myRange, 2) Step 10
    If myRange(1, c) = 1 Then
      For r = 2 To UBound(myRange, 2)
        myRange(r, c + 5) = .IfError(.Large(.Index(myRange, 0, c + 2), r - 1), "")
        myRange(r, c + 6) = .IfError(.Small(.Index(myRange, 0, c + 2), r - 1), "")
      Next
    End If
  Next
  .ScreenUpdating = False
  UsedRange = myRange
  .ScreenUpdating = True
  End With
End Sub
 
Upvote 0
Then this should work:
VBA Code:
Private Sub Worksheet_Calculate()
  Dim myRange As Variant
  Dim c As Long, r As Long

  myRange = UsedRange
  With Application
  For c = 1 To UBound(myRange, 2) Step 10
    If myRange(1, c) = 1 Then
      For r = 2 To UBound(myRange, 2)
        myRange(r, c + 5) = .IfError(.Large(.Index(myRange, 0, c + 2), r - 1), "")
        myRange(r, c + 6) = .IfError(.Small(.Index(myRange, 0, c + 2), r - 1), "")
      Next
    End If
  Next
  .ScreenUpdating = False
  UsedRange = myRange
  .ScreenUpdating = True
  End With
End Sub
Will this code execute the required action for all the 12 ranges OR some modification in the code is suggested, Flashbond
 
Upvote 0
Oh sorry,

Try to change this line:
VBA Code:
For r = 2 To UBound(myRange, 2)
Like this:
VBA Code:
For r = 2 To UBound(myRange, 1)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top