ThisWorkbook vs ActiveWOrkbook

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
ActiveWorkbook is as it says the workbook that is active and ThisWorkbook is the workbook the code resides in.

Post the code you are using that fails if you can't see the issue.

Please note that I am not hone to see the video.
 
Upvote 0
lezawang

Can you post the code that's causing the problem?

Mark

I was going to watch the video, kind of bored - don't ask, but it's more than 10 minutes long.:eek:
 
Upvote 0
but it's more than 10 minutes long.

Too long to borher watching on my phone then :) although I speculate that it probably isn't a major problem with the code, just that the workbook with the code isn't active when it is run... but we will see.
 
Upvote 0
I see your point of not watching the video. I will not post video link, really not good idea. I am attaching the code here. The code is reside inside a Module (Personal-->Module) could that be the problem? If I change everything to ActiveWork, I wont get any error msg

Sub test()
ThisWorkbook.Sheets("sheet2").Visible = False
ThisWorkbook.Sheets("sheet2").Visible = True
End Sub
 
Upvote 0
As I have stated ThisWorkbook is the workbook that that holds the code and so you are asking the macro to run on the Personal workbook and not the workbook that is active.
You are probably getting the error message because you don't have a Sheet2 in the Personal workbook.

If I change everything to ActiveWork, I wont get any error msg

Rather than ask why not test it?
 
Upvote 0
Thank you, yes you are right. I do not have sheet2 inside Personal project. Please see my project
screen shot below

Image - TinyPic - Free Image Hosting, Photo Sharing & Video Hosting

Now I want to know how can I create Sheet2 under Personal so I can run that code?
Thank you once again

As I have stated ThisWorkbook is the workbook that that holds the code and so you are asking the macro to run on the Personal workbook and not the workbook that is active.
You are probably getting the error message because you don't have a Sheet2 in the Personal workbook.



Rather than ask why not test it?
 
Upvote 0
You don't run the code on the personal workbook, you put the code in the personal workbook but it runs on either the active workbook or a specific workbook and you change the code to suit.

I think you either don't understand the purpose of the personal workbook or don't understand how you use it.
 
Upvote 0
Going back to your question in your first post...

Why the author can use "thisWorkbook" but I can not?

Because he hasn't got the code in the personal workbook, he has it in a regular workbook called "ExcelVBAIsFun lesson 8.xlsm"

If you had told us from the start that you had the code in the personal workbook I wouldn't have suggested ThisWorkbook I would have suggested ActiveWorkbook as it is most common to run the code on the ActiveWorkbook as below...

Code:
Sub test()
  ActiveWorkbook.Sheets("sheet2").Visible = False
  ActiveWorkbook.Sheets("sheet2").Visible = True
End Sub

The personal workbook is purely there to hold macros to run on other workbooks and so you wouldn't use ThisWorkbook.

Hopefully it is clearer now.
 
Last edited:
Upvote 0
Thanks a lot for your help. You are right. I started using Personal workbook because I recorded Marcos and then started editing them and delete the whole code and start over. You are right I am not fully understanding Personal workbook. I will read your reply again and again to get better understating. thanks once again.
Going back to your question in your first post...



Because he hasn't got the code in the personal workbook, he has it in a regular workbook called "ExcelVBAIsFun lesson 8.xlsm"

If you had told us from the start that you had the code in the personal workbook I wouldn't have suggested ThisWorkbook I would have suggested ActiveWorkbook as it is most common to run the code on the ActiveWorkbook as below...

Code:
Sub test()
  ActiveWorkbook.Sheets("sheet2").Visible = False
  ActiveWorkbook.Sheets("sheet2").Visible = True
End Sub

The personal workbook is purely there to hold macros to run on other workbooks and so you wouldn't use ThisWorkbook.

Hopefully it is clearer now.
 
Upvote 0

Forum statistics

Threads
1,223,989
Messages
6,175,814
Members
452,672
Latest member
missbanana

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