if sheet name is taken, add number at the end

Twollaston

Board Regular
Joined
May 24, 2019
Messages
241
Hey everyone,

I have a macro that creates a data table on one sheet and a pivot table on another sheet, and the problem I'm having is that i can only run the macro once and then I have to change the sheet names to do it again. I'm trying to make it so that if the sheet name is taken, then it will add a 1,2,3,4,5 etc... at the end, depending on how many times it's run.

If anyone can help me ammend my code that would be greatly appreciated.

I was hoping to do this for both sheet "Data" and "Summary"

Code:
Sub PivotTableCreation()


'Copies Query To New Sheet


ActiveSheet.Cells.Select
Application.CutCopyMode = False
    Selection.Copy
    Sheets.Add.Name = "Data"
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, transpose:=False


' Creates Pivot Table With Dynamic Range


    Rows("1:4").Select
    Selection.Delete Shift:=xlUp
    Range("A1").Select


'Dynamic Range Table
ActiveSheet.ListObjects.Add(xlSrcRange, Range([A1].End(xlDown), [A1].End(xlToRight)), , xlYes).Name _
= "NewTable"


'Table Style
ActiveSheet.ListObjects("NewTable").TableStyle = "TableStyleMedium17"




'New Sheet For Pivot Table
Sheets.Add.Name = "Summary"


'Pivot Table Creation
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="NewTable", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="Summary!R3C1", TableName:="InsertNameHere", DefaultVersion:=xlPivotTableVersion14






End Sub
 
Last edited:
Glad you figured it out.
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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