previous xlsb macro not working in xlsm workbook

craigexcel

Active Member
Joined
Jun 28, 2006
Messages
298
Office Version
  1. 2016
Platform
  1. Windows
Using Office 365 in the office.

I have a workbook with 1 sheet for dumping raw data to (Copy / Paste), along with 8 other sheets for operational States' data, for which relevant data is displayed via FILTER function formula.
At each quarter, to save each States' data in its own separate workbook, I was previously running my VBA from my PERSONAL.XLSB.
In the interest of making the (initial) data workbook a standalone workbook, I saved the workbook as .xlsm, and included the code in "This Worksheet".

Now, of course, there seems to be an issue with the following, where I add a new Sheet to the workbook, and then build up 2 Pivot Tables, starting with the one referenced below, which worked fine when run from PERSONAL.XLSB:


VBA Code:
        Sheets.Add       'this line executes, but no Sheet is added to the workbook
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _             'this line causes "Run-time error '5':  Invalid procedure call or argument"
            "Sheet1!R1C1:R" & iLastRow & "C19", Version:=8).CreatePivotTable TableDestination:= _
            "Sheet2!R3C1", TableName:="pivot_mgr", DefaultVersion:=8

I'd appreciate your insight as to the cause and necessary fix. Thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I continued tinkering with this problem to see if I could resolve it. I have hit on a solution, though I'm uncertain as to why it worked.
I moved the code from "ThisWorkbook", to "Module1".
Even though I only provided a small part of the code, given this 'solution', if anyone can offer some kind of explanation, I'd still appreciate it.
 
Upvote 0
The ThisWorkbook object has a Sheets property. Since you didn’t specify a workbook when you used Sheets.Add it defaults to ThisWorkbook.Sheets.Add and not ActiveWorkbook.Sheets.Add
 
Upvote 0
Solution
The ThisWorkbook object has a Sheets property. Since you didn’t specify a workbook when you used Sheets.Add it defaults to ThisWorkbook.Sheets.Add and not ActiveWorkbook.Sheets.Add
Thx RoryA. I appreciate you taking the time to share your insight!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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