VBA creating a new tab help

khalk

New Member
Joined
Mar 9, 2016
Messages
2
Hey guys first post, sorry for any transgressions. So, I'm trying to copy the results from a pivot table to a new tab that is created and named by the macro. The problem is coming with the creation and naming of the tab. After the first run it returns with an out of range function. I understand this is because when I recorded the macro it set the new tab as "Sheet2" and the new sheets are going to be "sheets3"etc. My seconded question is it possible to name a new tab from the contents from a cell in the pivot table. Is this possible?

Here is what I have so far.
Code:
Sub Attempt1()
'
' Attempt1 Macro
'

'
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets("Questions").Select
    Range("B2").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Sheets("Sheet2").Name = "ATM"
    Sheets("Questions").Select
    Range("A4:C6").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("ATM").Select
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Questions").Select
    Application.CutCopyMode = False
    Range("C5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
End Sub
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Dear khalk,

I would suggest that you name your new sheet immediately so you can refer back to it easily, using any cell you want. For example, the below code will name your new sheet to whatever is in Range A1 of your Questions sheet.
Put this after your first line of code "Sheet.Add"

Code:
ActiveSheet.Name = Sheets("Questions").Range("A1").Value
 
Upvote 0
On the assumption you only have one pivot table on the Questions sheet and you want to copy and then clear it, I'd suggest something like this:

Code:
Sub Attempt1()

    Dim ws                    As Worksheet
    Dim PT                    As PivotTable

    Set PT = Sheets("Questions").PivotTables(1)

    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
    ws.Name = "ATM"
    PT.TableRange2.Copy
    ws.Range("C1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                                SkipBlanks:=False, Transpose:=False
    PT.TableRange2.Clear
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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