Comparing historical data

taher9990

New Member
Joined
Feb 6, 2016
Messages
21
Dears

I need your help and support on this please

I am trying to fnd a formula or VBA code or using pivot table or even excel 2016 pivot table to do the following:

To compare grades for each ID and show the result in the result column, if grade for ID 1 in 04/01/2016 greater than grade for ID 1 in 03/01/2016 then show something like. True. and so on so forth for each row and ID.

Date
Id
GradeResult
01/01/2016
12
02/01/201614
03/01/201617
04/01/201619
01/01/201623
02/01/20162
7
03/01/201629
04/01/201622
01/01/201634
02/01/201636
03/01/2016310
04/01/201634

<colgroup><col><col span="3"></colgroup><tbody>
</tbody>
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I have a macro that I think will do what you want. In my example, I list the id and times to repeat in separate columns. When you run the macro, select the id and times to repeat values. In my sheet you would select f2:g4, then you will be asked to select a column for the id's. Select b2. I also assumed you were comparing only the same id's. Here is the screenshot before, followed by macro, and then screenshot after macro runs. Macro credit goes to kutools for excel. I modified one of their macros.


<b>Excel 2012</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #BBB"><colgroup><col width="25px" style="background-color: #DAE7F5" /><col /><col /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #DAE7F5;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th><th>G</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: center;;">Date</td><td style="text-align: center;;">Id</td><td style="text-align: center;;">Grade</td><td style="text-align: center;;">Result</td><td style="text-align: right;;"></td><td style="text-align: center;;">Id</td><td style="text-align: center;;">Repeat</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style="text-align: center;;">1/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">2</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: center;;">1</td><td style="text-align: center;;">4</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style="text-align: center;;">2/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">4</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: center;;">2</td><td style="text-align: center;;">4</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style="text-align: center;;">3/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">7</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: center;;">3</td><td style="text-align: center;;">4</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: center;;">4/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">9</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="text-align: center;;">1/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">3</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: center;;">2/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">7</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: center;;">3/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">9</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="text-align: center;;">4/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">2</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="text-align: center;;">1/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">4</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style="text-align: center;;">2/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">6</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style="text-align: center;;">3/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">10</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style="text-align: center;;">4/1/2016</td><td style="text-align: center;;"></td><td style="text-align: center;;">4</td><td style="text-align: center;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />

Macro:
Code:
Sub Compare_Historical_Data()

Dim Rng As Range, i As Integer
Dim InputRng As Range, OutRng As Range
Dim firstRow As Long, lastRow As Long

Range(Range("B2"), Range("B2").End(xlDown)).Select
Selection.ClearContents
Range("E1").Select

Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Select Id and Times to Repeat:", InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Select Column for Id:", Type:=8)
Set OutRng = OutRng.Range("A1")

For Each Rng In InputRng.Rows
    xValue = Rng.Range("A1").Value
    xNum = Rng.Range("B1").Value
    OutRng.Resize(xNum, 1).Value = xValue
    lastRow = Cells(1048576, 2).End(xlUp).Row
    firstRow = lastRow - (xNum - 1)
        For i = lastRow To firstRow Step -1
            If Cells(i, 3).Value > Cells(i - 1, 3).Value Then
                Cells(i, 4).Value = True
                Else
                Cells(i, 4).Value = False
            End If
        Next i
    Set OutRng = OutRng.Offset(xNum, 0)
Next

End Sub

After macro runs:


Excel 2012
ABCDEFG
1DateIdGradeResultIdRepeat
21/1/201612FALSE14
32/1/201614TRUE24
43/1/201617TRUE34
54/1/201619TRUE
61/1/201623FALSE
72/1/201627TRUE
83/1/201629TRUE
94/1/201622FALSE
101/1/201634TRUE
112/1/201636TRUE
123/1/2016310TRUE
134/1/201634FALSE
Sheet1
<p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br />
 
Upvote 0
Sorry, need to correct macro above. Here is the corrected version:

Code:
Sub Compare_Historical_Data()

Dim Rng As Range, i As Integer
Dim InputRng As Range, OutRng As Range
Dim firstRow As Long, lastRow As Long

Range(Range("B2"), Range("B2").End(xlDown)).Select
Selection.ClearContents
Range("E1").Select

Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Select Id and Times to Repeat:", InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Select Column for Id:", Type:=8)
Set OutRng = OutRng.Range("A1")

For Each Rng In InputRng.Rows
    xValue = Rng.Range("A1").Value
    xNum = Rng.Range("B1").Value
    OutRng.Resize(xNum, 1).Value = xValue
    lastRow = Cells(1048576, 2).End(xlUp).Row
    firstRow = lastRow - (xNum - 2)
        For i = lastRow To firstRow Step -1
            If Cells(i, 3).Value > Cells(i - 1, 3).Value Then
                Cells(i, 4).Value = True
                Else
                Cells(i, 4).Value = False
            End If
        Next i
    Set OutRng = OutRng.Offset(xNum, 0)
Next

End Sub
 
Upvote 0
Here is a screenshot for macro in post #3:


Excel 2012
ABCDEFG
1DateIdGradeResultIdRepeat
21/1/20161214
32/1/201614TRUE24
43/1/201617TRUE34
54/1/201619TRUE
61/1/201623
72/1/201627TRUE
83/1/201629TRUE
94/1/201622FALSE
101/1/201634
112/1/201636TRUE
123/1/2016310TRUE
134/1/201634FALSE
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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