pi>Visible <Type Mismatch>

interface

New Member
Joined
Jul 10, 2019
Messages
6
Respected gurus, experts and MVPs,


I have written the following code for the pivot table and it is generating pi.Visible <Type Mismatch> error.
Code:
Sub piVisibleMismatch()
Dim lastrow As Long, endrow As Long
Dim Psheet As Worksheet, Dsheet As Worksheet
Dim Pcache As PivotCache, Ptable As PivotTable, Prange As Range
Dim i As Long, j As Long, pi As PivotItem
Dim currep_date As Date
Application.ScreenUpdating = False

currep_date = "31/10/2019"
Worksheets("Summary").UsedRange
Worksheets("PivotTable").Activate
ActiveSheet.Cells.ClearContents
ActiveSheet.Cells.ClearFormats
ActiveSheet.Tab.ColorIndex = xlColorIndexNone
Set Psheet = Worksheets("PivotTable")
Set Dsheet = Worksheets("Summary")
lastrow = Dsheet.Cells(Rows.Count, 1).End(xlUp).Row
Set Prange = Dsheet.Range("A2:O" & lastrow)
On Error Resume Next
Set Pcache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Prange). _
CreatePivotTable(TableDestination:=Psheet.Cells(6, 1), TableName:="Total_Table")
Set Ptable = Pcache.CreatePivotTable(TableDestination:=Psheet.Cells(6, 1), TableName:="Total_Table")
With ActiveSheet.PivotTables("Total_Table").PivotFields("Balance 2")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlSum
End With
With ActiveSheet.PivotTables("Total_Table").PivotFields("Type of Account")
    .Orientation = xlPageField
    .Position = 1
End With
With ActiveSheet.PivotTables("Total_Table").PivotFields("Collection Date")
    .Orientation = xlPageField
    .Position = 3
End With
With ActiveSheet.PivotTables("Total_Table").PivotFields("Reporting CCY")
    .Orientation = xlColumnField
    .Position = 1
End With
ActiveSheet.PivotTables("Total_Table").PivotFields("Type of Account").CurrentPage = "Regular"

With ActiveSheet.PivotTables("Total_Table").PivotFields("Collection Date")
    For Each pi In .PivotItems
        If DateValue(pi.Name) > currep_date + 30 Then
            pi.Visible = False
        End If
    Next pi
End With
On Error GoTo 0
ActiveSheet.PivotTables("Total_Table").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Total_Table").TableStyle2 = "PivotStyleLight2"
ActiveSheet.UsedRange
ActiveSheet.Range("B:F").Select
Selection.Style = "Comma"
    Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
    Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
ActiveSheet.Cells.Font.Name = "Arial"
ActiveSheet.Cells.Font.Size = 8
ActiveSheet.Range("A:A").ColumnWidth = 35#: ActiveSheet.Range("B:F").ColumnWidth = 15#
ActiveSheet.Range("A1").Select
On Error GoTo 0
Application.ScreenUpdating = True
End Sub


I had a similar error for the 2 other worksheets but after debugging for more than 5 hours, I could trace the lastrow mismatch error. The lastrow in the source worksheet for the Pivot Table was updated consisting of rows with irrelevant data inadvertently while debugging. When I noticed the range of data for Pivot Table and last row number different I deleted the irrelevant data, ran macro which updated the lastrow correctly and the filtered the date correctly.


In the existing worksheet, the dates in the source worksheet are in date data type and the currep_date variable is set in date data type. I have been scratching my head for more than I did for before and researching articles in many websites to identify the error but I am still clueless.


I appreciate for your time and sharing your valuable knowledge and request for any alternative solution to filter dates without changing the date datatype. When I changed the date to "General" NumberFormat it worked.

Kind regards,

Roshan Shakya
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Cross posted https://www.excelforum.com/excel-pr...865-pi-visible-type-mismatch.html#post5226436

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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