VBA: using range to cycle through pivot items

Joe_L

New Member
Joined
Mar 10, 2011
Messages
14
Hi everyone - I currently have a macro where I loop through a range of cells to select items in a pivot table. My problem is if I mistype or fat finger an entry in my range, it overwrites whatever pivot item is in the pivot table and gives me mislabled and usually inaccurate data.

My question is, is it possible to tell my macro to return an error/stop running rather than overwrite the pivot item?

I've searched for this extensively and have been unable to find so I may just need to figure something else out, but figured I'd ask myself to see if anybody might have an idea.

I'm using excel 2007. Here is an general example of the current coding:

'VARIABLES AND LABELS

Dim pt As PivotTable
dim report as long
Set pt = Sheets("Pivot Sheet").PivotTables(1)

Dim rangename1 As String
Dim rangename2 As String

'PIVOT TABLE SELECTIONS

Dim queryct As String
Sheets("Range Sheet").Select
queryct = Application.WorksheetFunction.CountA(Range("A:A"))

For report = 2 To queryct Step 1

rangename1 = Sheets("Range Sheet").Cells(report, 4) & ""
rangename2 = Sheets("Range Sheet").Cells(report, 5) & ""

pt.PivotFields("Pivot Item 1").CurrentPage = rangename1
pt.PivotFields("Pivot Item 2").CurrentPage = rangename2

Thanks as always-
Joe
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Are you wanting to loop through all items of a PivotTable?
 
Upvote 0
Hi Glenn - right now this macro selects whatever pivot item I type into a data range on another tab so it's not usually every pivot item. My problem is if I mistype anything it overwrites the pivot item names when it tries to select. Just wondering if there is a way to prevent this from happening.
 
Upvote 0
Ensuring that data validation is not deleted

ok, I gave up on the above idea and just validated each column in the data range using lists. Each column (C to Z) has a different list referenced on another tab. I tried to modify the below seemingly common code within the worksheet to prevent the lists to be overwritten by pasting. Problem now is it seems to work for only 1 column. If I try to apply to the entire range (C2:Z500) I get an "out of stack space" error in Visual Basic and then my workbook crashes. Anybody ever experience this before? Thanks

Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("C2:D500")) Then
Exit Sub
Else
Application.Undo
MsgBox "Your last operation was canceled." & _
"It would have deleted data validation rules.", vbCritical
End If
End Sub
Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
x = r.Validation.Type
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
 
Upvote 0
I managed to find a solution toying around with the code, and since I've seen this question out there but never found the answer in my searches I'll go ahead and post this up incase anybody like me stumbles on it - and as usual for a novice like me, it's really simple.

Private Sub Worksheet_Change(ByVal Target As Range)
'Does the validation range still have validation?
If HasValidation(Range("C2:C500")) And HasValidation(Range("D2:D500")) And HasValidation(Range("E2:E500")) And etc..... Then
Exit Sub
Else
Application.Undo
MsgBox "Your last operation was canceled." & _
"It would have deleted data validation rules.", vbCritical
End If
End Sub
Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
x = r.Validation.Type
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
 
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