Compile Error: Sub or Function not defined when recording a macro to create a Pivot Table

cmvirginia7

New Member
Joined
Nov 24, 2015
Messages
1
Hello Everyone!

I just joined this forum and would love to hear your thoughts on an issue I'm having. I have been searching online and in an Excel Macro book, but cannot figure this out. I have a feeling there is a very simple solution that I'm just missing.

Software Version: Excel 2013, Windows 7

End Goal: I want to record a macro that creates a Pivot Table. I will start with a tab of raw data and then the macro will use a dynamic range to make the Pivot Table. I do not want to put the raw data into a table first because the data set is way too large. For the purposes of trying to figure out how to do this, I'm using a sample small data set right now.

My Code:
Code:
Sub Macro8()
'
' Macro8 Macro
'

'
    DataSheet ActiveSheet.Name
    Sheets.Add
    NewSheet ActiveSheet.Name
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        DataSheet & "!R1C1:R9C4", Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:=NewSheet & "R3C1", TableName:="PivotTable5", DefaultVersion _
        :=xlPivotTableVersion15
    Sheets(NewSheet).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
        "PivotTable5").PivotFields("Order Amt"), "Sum of Order Amt", xlSum
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Location")
        .Orientation = xlColumnField
        .Position = 1
    End With
Error Message:
I am receiving the following error message: Compile error: Sub or Function not defined. When I run the debugger, it highlights the very first line "Sub Macro8 ()".

If anyone has any thoughts, I would greatly appreciate it. I have been spending hours trying to figure out how to make this Pivot Table macro and it's driving me crazy. I'm also brand new to VBA and am reading the 101 Ready-To-Use Excel Macros book, but I can't find anything specific to my issue in there.

Thank you!

Warm regards,

Courtney
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Missing an = sign.

DataSheet = ActiveSheet.Name


Without the equal sign, the VBA compiler thinks DataSheet is a missing sub or custom function.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
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