简体   繁体   中英

Pivot table field with multiple field VBA

I am trying to do select the years between a range for a pivot table filter. For example: 1980 to 1990. The range will be given by the user input as startyear and endyear. I tried using for loop for it and it takes too long. It also only selects the year 1989 and 1990. This is the code:

For x = startyear To endyear
     For Each pvtItem In pvt.PivotFields("Build Year").PivotItems
           If pvtItem.Name = startyear Then
            
               On Error Resume Next
              pvtItem.Visible = True
                
         Else
              On Error Resume Next
              pvtItem.Visible = False
                
         End If
       Next pvtItem
 startyear = startyear + 1
Next x

Thank you for your help!

Could you please check your data type, pvtItem.Name will return string so you need to make both data type same. Either string or integer.

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