ActiveSheet Command not working in Excel Add-In

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I am using an add-in with saved subs to run code. The subs are being run from the ribbon. When i try the following code:

MsgBox ThisWorkbook.ActiveSheet

I get an error: "Run-Time error 91. Object variable or with block variable not set."

I have the 'control as iRibbonControl' correctly inserted to call the ribbon.

Why won't this work?

(FYI, even though I add new worksheets via the previous sub (which all works fine), the worksheets don't actually appear in the workbook. It just shows "sheet1" and "thisworkbook" with no additional sheets....
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This is the crux of it: The file with the code is the add-in, hence ThisWorkbook is the add-in file.

Maybe you want ActiveWorkbook instead? Or a particular, open (not add-in) workbook.

The error is because ThisWorkbook is the add-in file, and it won't have an ActiveSheet.
Adding sheets to ThisWorkbook won't appear because the add-in isn't visible - look in the VBE (Visual Basic Editor) and you should see the added sheets in the add-in file: they have been added there, not to the open workbook to which you want to add them.
 
Upvote 0

Forum statistics

Threads
1,215,746
Messages
6,126,645
Members
449,325
Latest member
Hardey6ix

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