Space in a sheet name throws off my macro

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
The below code works perfectly.
However I prefer to name my sheet "CX Pivot" and not "CXPivot"
But if I have that space it stops by creating the pivot table with error message "Runtime error 5, invalid procedure call or argument"
Is there any way around it?
VBA Code:
Sub cxp()
    ActiveSheet.NAME = "CX Report"
    Sheets.Add After:=ActiveSheet
    ActiveSheet.NAME = "CXPivot" 'I want to name it CX Pivot
    Sheets("CX Report").Select
    With ActiveSheet
        lastrow = .Cells(.Rows.Count, "E").End(xlUp).Row
    End With
    With ActiveSheet
        lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
    End With
    Pivot2 = "R1C1:R" & lastrow & "C" & lastcol
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Pivot2, Version:=6).CreatePivotTable _
    TableDestination:="CXPivot!R3C1", TableName:="CX", DefaultVersion:=6 
    'Table Destination would need to be "CX Pivot!R3C1" but it doesn't work if I do that
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,215,766
Messages
6,126,758
Members
449,336
Latest member
p17tootie

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