简体   繁体   中英

Excel VBA Pivot ISO week selection

I am using the code:

Sub weekselect()
Dim CurWkNum As Integer: CurWkNum = 31
Dim pvtI As PivotItem

With Worksheets("PIVOT 4").PivotTables(1)
    .PivotCache.Refresh
    .ClearAllFilters
    For Each pvtI In .PivotFields("WEEK_NUMBER").PivotItems
        If pvtI > CurWkNum Then
            CurWkNum = pvtI 
        End If
    Next pvtI
    For Each pvtI In .PivotFields("WEEK_NUMBER").PivotItems
        pvtI.Visible = (CInt(pvtI.Name) = CurWkNum)
    Next pvtI
End With

End Sub

This is selecting the last week in the pivot and works great for the previous requirements. However, I now want to be able to select current week and or current week and 4 weeks in the future, based on current ISO week.

Any help would be greatly appreciated!

Many thanks!

Any good Samaritans out there? I am hopeless with VBA and have searched the internet for this and asked on several forums. I was hoping this would be a simple edit. I managed to work around some of my original requirements and now only require the macro to select as above plus the next 4 weeks data. So I guess something like:

If pvtI > CurWkNum Then
CurWkNum = pvtI +1
CurWkNum = pvtI +2
CurWkNum = pvtI +3
CurWkNum = pvtI +4

Really stuck with this so would appreciate if any VBA pros out there could take pity on me and point me in the right direction.

Ok So far I have got this:

Sub Test() Dim ISOweekNum AsInteger Dim pvtI As PivotItem Dim ws As Worksheet

ForEach ws In ThisWorkbook.Worksheets(Array("PIVOT", "PIVOT 2", "PIVOT 3", "PIVOT 4")) With ws.PivotTables(1)
            .PivotCache.Refresh
            .ClearAllFilters
        ISOweekNum = DatePart("ww", Date, vbMonday, vbFirstFourDays)

ForEach pvtI In .PivotFields("WEEK_NUMBER").PivotItems
            pvtI.Visible = (CInt(pvtI.Name) >= ISOweekNum And CInt(pvtI.Name) <= ISOweekNum + 4) Next pvtI EndWith Next ws EndSub

Onedrivexlsworkbooksample

I cant get it to work yet though but believe its getting close. I have attached a sample workbook with the above as module1.

Any pros out there could spare 5 mins to put me right would be appreciated as I am struggling to bungle through this.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM