error

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

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Error 400

Error 400

resolvedResolved · Medium Priority · Version 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:

Some examples of CTRL key shortcuts in Excel 2010

Did you know that the old CTRL key shortcuts haven't changed from previous versions of Excel to the 2010 version?

They remain exactly the same:

Ctrl+B for bold
Ctrl+I for italics
Ctrl+P to Print
Ctrl+S to Save

View all Excel hints and tips


Server loaded in 0.09 secs.