Forum Discussion
Highest Lowest price from running price
- Jun 01, 2021
Start by entering -1000000 in M10 and 1000000 in M12. These are initial values that will be overwritten.
Enter the formula =NOW() in another cell (it doesn't matter where).
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:
Private Sub Worksheet_Calculate() Application.EnableEvents = False Range("M10").Value = Application.Max(Range("L9,M10")) Range("M12").Value = Application.Min(Range("L9,M12")) Application.EnableEvents = True End SubSwitch back to Excel.
Save the workbook as a macro-enabled workbook.
Make sure that you allow macros when you open it.
Try this version:
Private Sub Worksheet_Calculate()
Dim r As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
For r = 9 To 19
If Range("L" & r).Value <> "" Then
If Range("M" & r).Value = "" Then
Range("M" & r).Value = Range("L" & r).Value
Range("O" & r).Value = Now
ElseIf Range("L" & r).Value > Range("M" & r).Value Then
Range("M" & r).Value = Range("L" & r).Value
Range("O" & r).Value = Now
End If
End If
If Range("K" & r).Value <> "" Then
If Range("N" & r).Value = "" Then
Range("N" & r).Value = Range("K" & r).Value
Range("P" & r).Value = Now
ElseIf Range("K" & r).Value < Range("N" & r).Value Then
Range("N" & r).Value = Range("K" & r).Value
Range("P" & r).Value = Now
End If
End If
Next r
Application.EnableEvents = True
Application.ScreenUpdating = True
End Subis it anyway possible to make in this way
Min profit 1 will always follow the Max Profit 2
it means MIN profit 1 should always have time stamp after the max profit timestamp
basically what i need. After going MAX profit2 how much min profit 1 goes down.
then i will get Difference between MAx prfit 2-Min profit 1=% of diff
- samnptiJul 19, 2021Brass Contributoryes sir,
I have tagged you in new discussion. - HansVogelaarJul 19, 2021MVP
Please start a new discussion if you have a new, unrelated question.
- samnptiJul 19, 2021Brass ContributorSir its working perfectly
thanks again.
I have a different issue .I mean different topic. may i post it here? - HansVogelaarJul 19, 2021MVP
Private Sub Worksheet_Calculate() Dim r As Long Dim f As Boolean Application.ScreenUpdating = False Application.EnableEvents = False For r = 9 To 133 f = False If Range("K" & r).Value <> "" Then If Range("M" & r).Value = "" Then Range("M" & r).Value = Range("K" & r).Value If Range("M" & r).Value >= 0.01 Then Range("N" & r).ClearContents f = True End If Range("O" & r).Value = Now ElseIf Range("K" & r).Value > Range("M" & r).Value Then Range("M" & r).Value = Range("K" & r).Value If Range("M" & r).Value >= 0.01 Then Range("N" & r).ClearContents f = True End If Range("O" & r).Value = Now End If End If If Not f Then If Range("L" & r).Value <> "" Then If Range("N" & r).Value = "" Then Range("N" & r).Value = Range("L" & r).Value Range("P" & r).Value = Now ElseIf Range("L" & r).Value < Range("N" & r).Value Then Range("N" & r).Value = Range("L" & r).Value Range("P" & r).Value = Now End If End If End If Next r Application.EnableEvents = True Application.ScreenUpdating = True End Sub - samnptiJul 19, 2021Brass ContributorM will changed by existing VBA code
- HansVogelaarJul 19, 2021MVP
Will the values in column M be entered manually, or are they the result of a formula, or are they changed by VBA code?
- samnptiJul 19, 2021Brass Contributor
Sir,
thank you for your code. But it is not working in my case.
Please find the attached.
My requirement is whenever M value will >1% it will clear N corresponding cell only once.
- HansVogelaarJul 19, 2021MVP
Try this:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range If Not Intersect(Range("A:A"), Target) Is Nothing Then Application.EnableEvents = False For Each rng In Intersect(Range("A:A"), Target) If rng.Value >= 0.01 Then rng.Offset(0, 1).ClearContents End If Next rng Application.EnableEvents = True End If If Not Intersect(Range("D:D"), Target) Is Nothing Then Application.EnableEvents = False For Each rng In Intersect(Range("D:D"), Target) If rng.Offset(0, -3).Value >= 0.01 Then rng.Offset(0, -2).ClearContents End If Next rng Application.EnableEvents = True End If End Sub - samnptiJul 19, 2021Brass Contributor
sir,
It is taking reference from D
see the attachment
up to A1 to A8 manual typing working .And it clear cell in B
but From A9 it is reference cell from D9 .If change in D9 B9 should clear
- HansVogelaarJul 19, 2021MVP
What is the formula in column A?
- samnptiJul 18, 2021Brass Contributor
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
If Target >= 1 / 100 Then
Range("B" & Target.Row).Clear
End If
End If
End SubSir how to change it to formula in "A"
only manual typing is clearing cell B.
want to clear B when A cell change from other reference cell.
something like below
Option Explicit
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If Not Intersect(Target, Range("A:A")) Is Nothing Then
If Target >= 1 / 100 Then
Range("B" & Target.Row).Clear
Application.EnableEvents = True
End Subbut its not working
- samnptiJul 15, 2021Brass ContributorOk sir.Thanks
- HansVogelaarJul 15, 2021MVP
I'd leave it as it is now.
- samnptiJul 15, 2021Brass ContributorSir,
I understand,
Just curiosity question.
Min can act after Max timestamp?
If Max timestamp changed Min will also change accordingly. and it will bring Min value after that maxtimestamp
I mean MinValue timestamp will be always later of Maxtimestamp.
Anyway sir thank you for your support. - HansVogelaarJul 15, 2021MVP
How would that work? Once you have a Min value you would never be able to set the Max value anymore, since that would make the Max timestamp later than the Min timestamp.