workbook vs thisworkbook and worksheets(1) vs activesheet

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I understand I can replace worksheets(1) with thisworkbook in the code below. I checked both Workbook and Thisworkbook and I found they are not identical 100%. So how can I replace one with another if they are not identical. Is it technically ok to replace one with another or I should put in my mind I will lose/gain something when I do so? Same apply to Worksheets(1) vs activesheet. Thank you very much.

Sub myfirst()
ThisWorkbook.ActiveSheet.Cells(2, 2).Font.Bold = True
End Sub

Sub myfirst()
workbooks(1).ActiveSheet.Cells(2, 2).Font.Bold = True
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
ThisWorkbook refers to the workbook containing the code that is running. There is no guarantee that that would be the same as Workbooks(1) or even Activeworkbook. Similarly Worksheets(1) and Activesheet are only the same if you know that the first sheet in the workbook has been activated before running the code.
 
Upvote 0
ThisWorkbook refers to the workbook from which the running code resides. Workbooks(1) is the workbook that was opened first in that instance of excel. The workbooks collection is the collection of all open workbooks. For example my workbook(1) at the moment is personal.xlsb. Its therefore possible that ThisWorkbook is the same workbook as Workbooks(1) but it isnt necessarily the case it is.
 
Upvote 0
Thanks for all your reply. I appreciate it. But what I wanted to say, using one over the other, will lose some functions/properties that available in one and another. thank you.
 
Upvote 0
No, a Workbook is a Workbook, and a Worksheet is a Worksheet. Although Activesheet might not be a worksheet of course...
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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