Get First, High, Low, Last value within a given period

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I would like to request assistance with a VBA solution. I would like to get the following information below for each month/year and append the information in cell AA2. The dates begin in column (D18 - until) and the values Column (P18 - until). My intention will be to graph the results.

Month/Year

First value
Highest Value
Lowest Value
last Value

I would like the output to be displayed below...

DateOpenHighLowClose
09/01/18-60.1808.21-112.9808.21
10/01/18-506.42784.1-779.41504.1
11/01/181981.631981.63466.1700.1

<tbody>
</tbody>


DatePrice
09/28/2018-60.2
09/28/2018-112.9
09/28/2018348.39
09/28/2018199.95
09/28/2018808.21
10/01/2018-506.4
10/01/2018-251.2
10/01/2018-779.4
10/01/2018758.36
10/30/20181504.1
11/01/20181981.63
11/02/2018466.1
11/02/2018700.1
12/07/2018486.69
12/08/2018332.1
12/10/2018-2377.9
12/10/2018-3023.46
12/10/2018-2422.9
12/10/2018-2867.9
12/10/2018-1898.31
12/10/20181927.1
12/11/2018-616.23
12/11/2018456.8
12/12/2018-1118.3
12/12/2018-1013.49
12/12/2018-819.9
12/12/2018-548.48
12/12/2018-387.9
12/12/2018-328.3
12/14/2018-518.32
12/14/2018-174.09
12/14/2018-84
12/20/2018-2523.12

<tbody>
</tbody>


Thank you kindly
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this


Code:
Sub Get_Values()
    Dim dt As Date, c As Range, f As Range
    Dim lr1 As Long, lr2 As Long, y As String, m As String
    
    Range("AA2:AE" & Rows.Count).ClearContents
    lr1 = Range("D" & Rows.Count).End(xlUp).Row
    For Each c In Range("D18", Range("D" & Rows.Count).End(xlUp))
        y = Year(c)
        m = Month(c)
        dt = DateSerial(y, m, 1)
        Set f = Range("AA:AA").Find(dt, LookIn:=xlValues, lookat:=xlWhole)
        If f Is Nothing Then
            lr2 = Range("AA" & Rows.Count).End(xlUp).Row + 1
            Range("AA" & lr2).Value = dt
            Range("AB" & lr2).Value = Cells(c.Row, "P")
            Range("AC" & lr2).Value = Evaluate("=MAX(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",P18:P" & lr1 & ")))")
            Range("AD" & lr2).Value = Evaluate("=MIN(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",P18:P" & lr1 & ")))")
            Range("AE" & lr2).Value = Cells(Evaluate("=MAX(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",ROW(P18:P" & lr1 & "))))"), "P")
        End If
    Next
End Sub
 
Upvote 0
DanteAmor...I sincerely thank you for your generous help. The code achieves the results, however, on first run, it does not achieve the results. on the second run, it works. Any reason?

Try this


Code:
Sub Get_Values()
    Dim dt As Date, c As Range, f As Range
    Dim lr1 As Long, lr2 As Long, y As String, m As String
    
    Range("AA2:AE" & Rows.Count).ClearContents
    lr1 = Range("D" & Rows.Count).End(xlUp).Row
    For Each c In Range("D18", Range("D" & Rows.Count).End(xlUp))
        y = Year(c)
        m = Month(c)
        dt = DateSerial(y, m, 1)
        Set f = Range("AA:AA").Find(dt, LookIn:=xlValues, lookat:=xlWhole)
        If f Is Nothing Then
            lr2 = Range("AA" & Rows.Count).End(xlUp).Row + 1
            Range("AA" & lr2).Value = dt
            Range("AB" & lr2).Value = Cells(c.Row, "P")
            Range("AC" & lr2).Value = Evaluate("=MAX(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",P18:P" & lr1 & ")))")
            Range("AD" & lr2).Value = Evaluate("=MIN(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",P18:P" & lr1 & ")))")
            Range("AE" & lr2).Value = Cells(Evaluate("=MAX(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",ROW(P18:P" & lr1 & "))))"), "P")
        End If
    Next
End Sub
 
Last edited:
Upvote 0
Is it possible to do this weeks? (i.e. find the same thing, but instead of months, use weeks)
 
Last edited:
Upvote 0
DanteAmor...I sincerely thank you for your generous help. The code achieves the results, however, on first run, it does not achieve the results. on the second run, it works. Any reason?

It can be the date format.
The date format of column D must be the same format in column AA (mm/dd/yyyy or dd/mm/yyyy)
 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

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