Select last 12 items in a drop down list

nguerra

New Member
Joined
Oct 1, 2013
Messages
46
Hoping someone can help with this topic. I have a drop down that has 24 items that are always rolling or being updated. I'm looking for a way to filter on the last 12.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Assuming that your items are in Sheet1, column A (starting in cell A1), you can create a Named Range using the following formula:

Code:
=OFFSET(Sheet1!$A$1,COUNTA(Sheet1!$A:$A)-12,0,12,1)

This will always "select" the last 12 items. You can then reference this Named Range in your drop-down list.

Let me know if it works for you.
 
Upvote 0
Which type of dropdown list is this ?
- data validation list
- worksheet form control LISTBOX \ COMBOBOX
- worksheet active-x control LISTBOX \ COMBOBOX
- userform LISTBOX \ COMBOBOX

What is the source of the 24 values ?
 
Upvote 0
Not sure how to incorporate that. The recorded macro looks like this:

ActiveSheet.PivotTables("PivotTable1").PivotFields("[Data].[Month].[Month]" _
).VisibleItemsList = Array("[Data].[Month].&[yr2018mth08]", _
"[Data].[Month].&[yr2018mth09]", "[Data].[Month].&[yr2018mth10]", _
"[Data].[Month].&[yr2018mth11]", "[Data].[Month].&[yr2018mth12]", _
"[Data].[Month].&[yr2019mth01]", "[Data].[Month].&[yr2019mth02]", _
"[Data].[Month].&[yr2019mth03]", "[Data].[Month].&[yr2019mth04]", _
"[Data].[Month].&[yr2019mth05]", "[Data].[Month].&[yr2019mth06]", _
"[Data].[Month].&[yr2019mth07]")

Only issue is, this will continually roll or update.
 
Upvote 0
Anyone able to take a stab at making the below recorded macro a VBA?

Not sure how to incorporate that. The recorded macro looks like this:

ActiveSheet.PivotTables("PivotTable1").PivotFields("[Data].[Month].[Month]" _
).VisibleItemsList = Array("[Data].[Month].&[yr2018mth08]", _
"[Data].[Month].&[yr2018mth09]", "[Data].[Month].&[yr2018mth10]", _
"[Data].[Month].&[yr2018mth11]", "[Data].[Month].&[yr2018mth12]", _
"[Data].[Month].&[yr2019mth01]", "[Data].[Month].&[yr2019mth02]", _
"[Data].[Month].&[yr2019mth03]", "[Data].[Month].&[yr2019mth04]", _
"[Data].[Month].&[yr2019mth05]", "[Data].[Month].&[yr2019mth06]", _
"[Data].[Month].&[yr2019mth07]")

Only issue is, this will continually roll or update.
 
Upvote 0
You would have been more likely to receive a more relevant response to your question if your thread title and post#1 had made it clear that this is a VBA and pivot table dropdown issue

The code below is untested with a pivot table. The generated string appears to be correct - however that does not guarantee that line for the dropdown will function correctly

The string generated by VBA with combination year 2019 and month 7 is this
"[Data].[Month].&[yr2018mth08]","[Data].[Month].&[yr2018mth09]","[Data].[Month].&[yr2018mth10]","[Data].[Month].&[yr2018mth11]","[Data].[Month].&[yr2018mth12]","[Data].[Month].&[yr2019mth01]","[Data].[Month].&[yr2019mth02]","[Data].[Month].&[yr2019mth03]","[Data].[Month].&[yr2019mth04]","[Data].[Month].&[yr2019mth05]","[Data].[Month].&[yr2019mth06]","[Data].[Month].&[yr2019mth07]"
( matches what you provided in post#4 )

Add worksheet named "Strings"
(illustrated below)
- VBA amends values in A2 and B2 (based on user input)
- everything else in the sheet is formula driven
- VBA then concatenates the strings found in D2:D13

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
1
Monthyear[Data].[Month].
2
7​
2019​
07[Data].[Month].&[yr2019mth07] formula in A3 copied down =MONTH(EDATE(DATE(B2,A2,1),-1))
3
6​
2019​
06[Data].[Month].&[yr2019mth06] formula in B3 copied down =YEAR(EDATE(DATE(B2,A2,1),-1))
4
5​
2019​
05[Data].[Month].&[yr2019mth05] formula in C2 copied down =TEXT(A2,"00")
5
4​
2019​
04[Data].[Month].&[yr2019mth04] formula in D2 copied down =$D$1&"&[yr" & B2 & "mth" & C2 & "]"
6
3​
2019​
03[Data].[Month].&[yr2019mth03]
7
2​
2019​
02[Data].[Month].&[yr2019mth02]
8
1​
2019​
01[Data].[Month].&[yr2019mth01]
9
12​
2018​
12[Data].[Month].&[yr2018mth12]
10
11​
2018​
11[Data].[Month].&[yr2018mth11]
11
10​
2018​
10[Data].[Month].&[yr2018mth10]
12
9​
2018​
09[Data].[Month].&[yr2018mth09]
13
8​
2018​
08[Data].[Month].&[yr2018mth08]
14
Sheet: Strings

VBA
Code:
Sub nguerra()
    Dim Q As String:     Q = Chr(34)
    Dim Mth As Long, Yr As Long, r As Long, ws As Worksheet, [COLOR=#ff0000]VSL[/COLOR] As String
    Set ws = Sheets("Strings")
[I][COLOR=#006400]'ask user and write to sheet[/COLOR][/I]
    Yr = InputBox("Year ?", "Which Year", 2019)
    Mth = InputBox("Month", "Which Month", 7)
    ws.Range("A2:B2") = Array(Mth, Yr)
[COLOR=#006400][I]'create array string[/I][/COLOR]
    VSL = Q & ws.Cells(13, 4) & Q
    For r = 12 To 2 Step -1
        [COLOR=#ff0000]VSL[/COLOR] = VSL & "," & Q & ws.Cells(r, 4) & Q
    Next r
[COLOR=#006400][I]'your code[/I][/COLOR]
    ActiveSheet.PivotTables("PivotTable1").PivotFields("[Data].[Month].[Month]").VisibleItemsList = Array([COLOR=#ff0000]VSL[/COLOR])
End Sub
 
Upvote 0
Thank you Yongle, I think you are correct in that the Title needed more detail. I thank you for your help below, not quite what I'm looking for where intervention is needed by the user. I will see if I can incorporate it somehow or re-ask the question. Thank you everyone!
 
Upvote 0
not quite what I'm looking for where intervention is needed by the user

So what are you looking for ? :confused:

If you want this to be automated then you need to tell us where the values for month and year are located so that VBA can use them
- probably a very minor mod required to previous code
 
Last edited:
Upvote 0
Well I receive a pivot table once a month. One of the filters has the options of:
yr17mth08,yr17mth09,yr17mth10,yr17mth11,yrmth12,
yr18mth01,yr18mth02,yr18mth03,yr18mth04,yr18mth05,yr18mth06,yt18mth07,yr17mth08,yr18mth09,yr18mth10,yr18mth11,yr18mth12,
yr19mth01,yr19mth02,yr19mth03,yr19mth04,yr19mth05,yr19mth06,yt19mth07.
however, every month the oldest field drops off and a new field is added. It's basically a rolling 24 months of data. The report generated monthly is only looking for the last 12 months. It's important to know that the values in the pivot table are not formatted as dates. I believe they are just strings.

<tbody>
</tbody>

However, Every month those options change. The oldest one drops off and a new one is added to the bottom.
 
Upvote 0
I expected your sheet to contain the values of latest month and year - but your narrative suggests that's not so

I am guessing that you need VBA
- to interrogate ActiveSheet.PivotTables("PivotTable1").PivotFields("[Data].[Month].[Month]").VisibleItemsList
- and determine the last 12 items
- and filter based on those

That is something I cannot help you with. I suggest you start a new thread with a helpful title, together with a concise but clear and full explanation of your requirement.
Good luck
 
Upvote 0

Forum statistics

Threads
1,214,571
Messages
6,120,302
Members
448,954
Latest member
EmmeEnne1979

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