Cannot Insert PivotTable in open sheet using VBA

Hello Experts,

I am new to VBA so please bear with me. I have a workbook with several worksheets, each for a different month. The sheets are also named after the respective months. I recorded a macro in the sheet for April where I performed a Pivot Table on the data in that sheet. When I try to run that macro to make a similar pivot table on a different sheet e.g May or June, I get an error that reads, "Run-time error "1004": Application-defined or object-defined error".
This is the highlighted code,

 ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "April!R2C1:R1343C6", Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:="April!R4C9", TableName:="PivotTable4", DefaultVersion:= _

Open in new window

I realized that the code is only executing/making a pivot for "April" but I want it to make the macro able to run on any open sheet, Not just April. I suspect that this is the reason I am getting the error but correct me if I'm wrong.

So how can I make the macro work on any sheet or more dynamic instead of specific to the April sheet.
Tj aAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        ActiveSheet.Name & "!R2C1:R1343C6", Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:=ActiveSheet.Name & "!R4C9", TableName:="PivotTable" & ActiveSheet.Index, DefaultVersion:= _

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tj aAuthor Commented:
When I try that I get an error that reads, "Run-time error '424': Object required"
Tj aAuthor Commented:
Actually it works. I copied that code incorrectly the 1st time. Thank you so much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.