If Then Function Error VBA

Lauren123

New Member
Joined
Mar 16, 2018
Messages
28
Hi, I have this code and it shows me "Object doesn't support this property or method" I dont know if anybody knows waht might be the mistake!


Sub Horror()


Call LastRowPivotTable
With ThisWorkbook
Set wb = ThisWorkbook
Set DR = wb.Sheets("Draft")
Set PT = wb.Sheets("Pivot Table I")
MasterAcct = wb.DR.Range("C4")


If wb.DR.Range("T7").Value = "1" Then

wb.PT.PivotFields("Master Account").ClearAllFilters
wb.PT.PivotFields("Master Account").CurrentPage = MasterAcct
Else
wb.PT.Range("B4:L & LastRowPivot").ClearContents
End If
End With


End Sub




This is the call I have
Sub LastRowPivotTable()


Dim ThisWK As String


ThisWK = ThisWorkbook.Name


LastRowPivot = Workbooks(ThisWK).Worksheets("Pivot Table I").Cells(Rows.Count, "B").End(xlUp).Row




End Sub

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I removed a lot of unecessary items from the code and put your last row variable in the main code instead of a macro call. See if it works for you.
Code:
Sub Horror2()
 Dim LastRowPivot As Long, DR As Worksheet, PT As Worksheet, MasterAcct As Variant
 'Set wb = ThisWorkbook
 Set DR = Sheets("Draft")
 Set PT = Sheets("Pivot Table I")
 MasterAcct = DR.Range("C4").Value
     If DR.Range("T7").Value = "1" Then
         LastRowPivot = Worksheets("Pivot Table I").Cells(Rows.Count, "B").End(xlUp).Row
         PT.PivotFields("Master Account").ClearAllFilters
         PT.PivotFields("Master Account").CurrentPage = MasterAcct
     Else
         PT.Range("B4:L & LastRowPivot").ClearContents
     End If
End Sub
 
Last edited:
Upvote 0
Hello! thanks for answering!

I did what you said and is showing "Method od data member not found" PT.PivotFields("Master Account").ClearAllFilters and selects the part i put on bold.

I dont know if you could help me fix that?

Thanks!
 
Upvote 0
Idon't work with pivot tables, but if I read the help file correctly, you probably need to add the PivotTable reference into these two lines of code.
Code:
[COLOR=#B22222][/COLOR]PT.[COLOR=#B22222]PivotTableRefHere.[/COLOR]Pi[COLOR=#B22222][/COLOR]votFields("Master Account").ClearAllFilters
[COLOR=#B22222][/COLOR]PT.[COLOR=#B22222]PivotTableRefHere[/COLOR].PivotFields("Master Account").CurrentPage = MasterAcct
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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