400 Error on Macro

tlc_in_OK

Board Regular
Joined
Jun 27, 2011
Messages
56
I have two macros, one is working fine and the other generates an error, and I can't figure out why. They both take filtered pivot table data and paste it to other columns (which we use to do calculations and a graph).

The first one changes the filter to "Accepted" status, and copies A6:Cx over to F2:Hx. Here's the code:

Sub CopyPivotInfo1()
Dim RngPS As Range, RngPCR As Range, Rng As Range, Last As Long
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Report_Status")
.PivotItems("Accepted").Visible = True
.PivotItems("No Bid-No Sale").Visible = False
.PivotItems("Rejected").Visible = False
With Sheets("Pivot")
Set RngPS = .Range(.Range("A6"), .Range("C" & Rows.Count).End(xlUp))
End With
With Sheets("Pivot")
Set Rng = .Range(.Range("F$2"), .Range("H" & Rows.Count).End(xlUp))
Rng.Resize(, 3).ClearContents
RngPS.Copy .Range("F2")
'Last = .Range("H" & Rows.Count).End(xlUp).Row
End With

End With
End Sub
This second one simply changes the filter on the pivot, selects the same cells as the sub above and should paste it to the next blank row in F (although I'm not sure I have the range right), but I keep getting a "400" error when I try to run it. Any ideas?

Sub CopyPivotInfo4()
Dim RngPS As Range
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Report_Status")
.PivotItems("Accepted").Visible = False
.PivotItems("No Bid-No Sale").Visible = True
.PivotItems("Rejected").Visible = True

Set RngPS = .Range(.Range("A6"), .Range("C" & Rows.Count).End(xlUp))
RngPS.Copy .Range("F" & Cells(Rows.Count, 6).End(xlUp).Offset(1, 0))

End With
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try the second one like:
Code:
Sub CopyPivotInfo4()
Dim RngPS As Range
With ActiveSheet
    With .PivotTables("PivotTable1").PivotFields("Report_Status")
        .PivotItems("Accepted").Visible = False
        .PivotItems("No Bid-No Sale").Visible = True
        .PivotItems("Rejected").Visible = True
    End With
    Set RngPS = .Range(.Range("A6"), .Range("C" & Rows.Count).End(xlUp))
    RngPS.Copy .Range("F" & Cells(Rows.Count, 6).End(xlUp).Offset(1, 0))
End With
End Sub
 
Upvote 0
I recommend you change the order of this block of code from
Code:
        .PivotItems("Accepted").Visible = False
        .PivotItems("No Bid-No Sale").Visible = True
        .PivotItems("Rejected").Visible = True
to
Code:
        .PivotItems("No Bid-No Sale").Visible = True
        .PivotItems("Accepted").Visible = False
        .PivotItems("Rejected").Visible = True

If the Accepted pivot item was the only one visible, then this line
.PivotItems("Accepted").Visible = False
in the first block would raise an error since at least one PivotItem must be visible.
 
Upvote 0
Thanks Guys, but it's still giving me the 400 error. Any other thoughts?

Changing the order of the pivot filters DID get it to finally filter the pivot, but for some reason, I'm still getting the 400 error. So it must be stemming from the copy/paste function. I can't see the error.
 
Last edited:
Upvote 0
What line of code shows the error? What is the text associated with Error 400?
Is row F (the insert point) inside the pivot table? Can you do manually what you are trying to do by code?
 
Upvote 0
It compiles, but simply gives me that horrid "400" error when I try to run it. When I try to step through it, it gives me a "Run-time error '1004': Application-defined or object-defined error" on the last "End With" statement.

BTW, "F" is NOT inside the pivot table, and I can do it manually, but I intend to hide this worksheet and have it all done in the background for end-users.
 
Upvote 0
If you turn on the macro recorder and perform the task that is failing manually what does the code show for that operation?
 
Upvote 0
Here is VBE Help on Error 400:

<TABLE id=topTable width="100%"><TBODY><TR id=headerTableRow2><TD align=left>Form already displayed; can't show modally (Error 400)</TD></TR></TBODY></TABLE>
You can't use the Show method to display a visible form as modal. This error has the following cause and solution:

  • You tried to use Show, with the style argument set to 1 – vbModal, on an already visible form. Use either the Unload statement or the Hide method on the form before trying to show it as a modal form.
Does it apply at all?
 
Upvote 0
If you turn on the macro recorder and perform the task that is failing manually what does the code show for that operation?


Here's what I get when I records the macro:

ActiveSheet.PivotTables("PivotTable1").PivotFields("Report_Status"). _
CurrentPage = "(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Report_Status")
.PivotItems("Accepted").Visible = False
End With
Range("A6:C74").Select
Selection.Copy
Range("F129").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
It doesn't include the "True" pivot selections at all. It selects the columns of filtered data in the pivot, and I copied the 3 columns in the pivot and pasted to the first blank cell in column "F" (happened to be 129 this time).
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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