Find item x in a list VBA

Confidentjohn

Board Regular
Joined
Mar 3, 2009
Messages
73
Hi

I want to control a filter in a pivot table with some VBA.

The data is a date but displayed in the following format

yyyymmdd and is represented as a number.


The pivot data looks roughly like

DateData
201402011
201402012
201402013
201402022
201402024
201402028
201402035
201402039
201402048
2014020490
201402056
201402058
201402069
201402063
201402063
201402077
201402076
201402076
201402085
201402083

<tbody>
</tbody>


In the actuall spreadsheet this is a constantly growing list...

When pivoted the date acts as a label and is displayed as below
20140205201402062014020720140208
Data7878956567

<tbody>
</tbody>


I would like to set some VBA that scans the list I and finds the maximum (Which i have figured out) and the x item, in the example above lets say the 4th back from the largest. But in reality will be the 28th item back.


I would then use these to dynamically set the filter between the start and end values. (This bit i can do)

Would someone help me figure out how to get the mid item so i can set a dimension with it in VBA


Hope someone can help

Cheers

John
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
If i understood correctly you want the 4th largest date (considering just unique values)

So assuming the dates in column A, header in row 1, maybe...

Using an array formula
=LARGE(IF(FREQUENCY(DataRange,DataRange),DataRange),4)

Confirmed with Ctrl+Shift+Enter

where DataRange is dynamic named range comprising the dates in column A

Or in VBA

Code:
Sub aTest()
    'Gets the 4th largest unique date in Range A2:A?
    Dim myDate As Long
    
    Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Name = "DataRange"
    myDate = Evaluate("=LARGE(IF(FREQUENCY(DataRange,DataRange),DataRange),4)")
    MsgBox myDate
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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