Run-time error '9' with ThisWorkbook.Sheets Function

shibbyz

New Member
Joined
Dec 29, 2013
Messages
6
I have run into the problem with ThisWorkbook.Worksheet function. I started doing tutorials by VBAIsFun on youtube and I am running into trouble with this function.

Here is the tutorial that I am having trouble with : Excel VBA Basics #7 - Using specific sheets, Hiding, Unhiding and Selecting with VBA - YouTube

So I got the Run-time error code '9' when I went to run it. I changed all of the Thisworkbook.sheets command to Activeworkbook.sheets command and it worked when I ran it from VBA. I then went and tried to run the code from the excel sheet (Alt+F8) and no macros came up as available to run.

This leads me to believe that there might be some sort of link between the VBA code and the excel sheet that is missing. Any thoughts or suggestions are greatly appreciated.

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
"Thisworkbook" always identifies the workbook in which the running macro is stored.
Activeworkbook can identify ANY workbook that is active at a particular point.
 
Upvote 0
How do I ensure that that macro is stored within the workbook? I opened Visual basic up from the excel sheet that I am working. Is there a specific set of steps that I need to take to associate the code with that specific workbook?
 
Upvote 0
if a given code is stored in workbook "A" then anytime you see in the code "thisworkbook", it is referring to workbook "A"

I like to SET all the worksheets that my code will use, for instance if i know that during a code i will access several times worksheet "Report" in workbook "B" I will
dim Rep as Worksheet
set Rep = workbooks("B.xls").Worksheets("Report")


you can also set workbooks the same way

this way whenever i type Rep I am easily accessing the exact sheet in the exact workbook that I want. It's good practice and makes your code easier to read
 
Upvote 0
Thanks a lot for the input. I had my sheets all messed up. I had to start over and now it is working. I'm not sure if it has something to do with the file format. I was using a file that had been converted from office 2007 to 2010. It seems to be working well for now.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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