Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the number of sheets in an active excel workbook 2

Status
Not open for further replies.

hengsin

Technical User
Mar 26, 2003
128
0
0
MY
hi, can i know how to get the number of sheets in an active excel workbook
 
Enter the following into a code module:

Code:
Sub SheetsNumber()
    msgbox ThisWorkbook.Worksheets.Count
End sub

I hope this helps!


Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
thanks, it's working. But when i modified your code before this:

Sub SheetsNumber()
msgbox ActiveWorkbook.Worksheets.Count
End sub

it's also working. I would like to know what are the different between ActiveWorkbook and ThisWorkbook.



 
Hi hegsin,

ActiveWorkbook refers to the workbook that is currently active (i.e. The workbook with the selected cells).

ThisWorkbook refers to the workbook in which the code that is currently running is located.

ThisWorkbook can also be (and in most cases is) the ActiveWorkbook.

If open and activate other workbooks using code from another workbook they become the ActiveWorkbook and the workbook that contains the code that opens and activates the other workbooks is always ThisWorkbook.

In your code, if you use ThisWorkbook you will only be able to count the worksheets in the workbook that is running that procedure. If you use ActiveWorkbook (as you have), then you will be able to count the worksheets in another workbook (if it is activated).

Depending on what you want to do, you can use either ThisWorkbook or ActiveWorkbook

I hope this helps!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top