Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

error

ResolvedVersion 2003

Samantha has attended:
Excel VBA Intro Intermediate course

Error 400

I'm trying to create a pivot table from a static piece of data and when I've tried to run it get an message box that pops up that says '400'.

The code is as follows:

Option Explicit

Dim strActive As String
Dim rngSource As Range
Dim rngDestination As Range
Dim pvtTable As PivotTable
Dim strPivot1 As String



Sub CreateSheet()

Sheets.Add Before:=Sheets(Sheets.Count)

Call DeleteSheet

ActiveSheet.Name = strPivot1

End Sub

Sub PivotActive()


Set rngDestination = Sheets(strPivot1).Range("A1")
Set rngSource = Sheets(strActive).Range("A1").CurrentRegion

ActiveSheet.PivotTableWizard SourceType:=xlDatabase, _
SourceData:=rngSource, tabledestination:=rngDestination, tablename:="TestPivot"

Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.ShowPivotTableFieldList = False

Set pvtTable = Sheets(strPivot1).PivotTables("TestPivot")

With pvtTable

.PivotFields("Consol.Category").Orientation = xlPageField
.PivotFields("Account Description").Orientation = xlRowField
.PivotFields("TC Description").Orientation = xlColumnField
.PivotFields("1011 Budget C").Orientation = xlDataField
.PivotFields("Sum of 1011 Budget C").Function = xlSum

End With

pvtTable.DataBodyRange.Select
Selection.NumberFormat = "[$£]#,##0"

Range("A1").Select

End Sub

Sub DeleteSheet()

Dim wsSheet As Worksheet

Application.DisplayAlerts = False

For Each wsSheet In ActiveWorkbook.Worksheets

If wsSheet.Name = strActive Then

wsSheet.Delete
Exit For

End If

Next wsSheet

Application.DisplayAlerts = True

End Sub

Sub MainPivotRpt1()

Call CreateSheet
Call PivotActive

End Sub

RE: Error 400

Hi Samatha

Thanks for your question

When your code crashes, does the VBE open? If so what line of code is highlighted as this will enable me to track down the error more precisely

Thanks

Stephen

RE: Error 400

Hi

It highlights:

ActiveSheet.Name = strPivot1

RE: Error 400

Hi Samantha

Thanks for your question

Sorry about the delay in responding, and it is possible that you have already found the solution

The code falls over when you try to name the sheet strPivot1. However, your delete sheet procedure deletes a sheet called strActive. So it is likely that you are trying to give the sheet a name that already exists. If you change strActive to strPivot1 you might find that this solves the problem

Regards

Stephen

Wed 25 Aug 2010: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Return to the active cell after scrolling

When I scroll a long way down the screen from a selected cell, I can return to that cell with the Ctrl+Back Space shortcut. The active cell now appears in roughly the middle of the screen.

Shift+Back Space does something similar. Scroll down from the active cell and Shift+Back Space returns me to it and puts the active cell at the top of the screen; scroll up from the active cell and Shift+Back Space returns me to it and puts the active cell at the bottom of the screen.

Note also, that while Ctrl+Back Space will return me back to a selected range, Shift+Back Space only ever returns me to the active cell, which is normally at the top left-hand corner of any selected range.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.