Trying to count what number a worksheet is in a workbook

melfinance

New Member
Joined
Jan 20, 2005
Messages
7
I need to count what number a worksheet is in the workbook. The first sheet should display 1 in cell a1. Sheet number 2 should display 2 in a1. Continued so if I insert a sheet the workbook is able to tell me the number of worksheets in front of it from sheet 1. Please help :rolleyes:
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
Put this code in your ThisWorkbook module:
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Application.EnableEvents = False
    Sh.Range("A1").Value = Sheets(ActiveSheet.Name).Index
    Application.EnableEvents = True
End Sub

Press Alt-F11 to open the VBE.
Press Control-R to open the Project Explorer.
Click "Microsoft Excel Objects" for the file you're working on.
Put the code in the correct sheet or module.
Open the Code pane with F7.
Paste the above code in.
Press Alt-Q to close the VBE and return to Excel.

Hope that helps!
 

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
Glad to help. Just remember it's there in case you ever want to turn it off! :eek:
 

Forum statistics

Threads
1,148,219
Messages
5,745,446
Members
423,952
Latest member
EduardoM

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
Top