简体   繁体   English

在单元格中添加工作表计数器

[英]Add a sheet counter in a cell

i have a excel file with a lot of sheets.我有一个包含很多工作表的 excel 文件。 i want to make a sheet counter to put in a specific cell of each one of those sheets "sheet x of Y" for example, if you are on the sheet 3 of 5, in the cell "X5" must to say "sheet 2" and in the cell "X6" say "of 5" each sheet have a button to add other sheet, so the sheet counter must be update every time you add a sheet我想制作一个表格计数器以放入每张表格“Y 表格 x”的特定单元格中,例如,如果您在 5 张表格中的第 3 张表格上,则在单元格“X5”中必须说“表格 2 ”并且在单元格“X6”中说“5”每张表都有一个添加其他表的按钮,因此每次添加表时必须更新表计数器

i tried this code but doesn't work, it only puts "0" in the first sheet我试过这段代码但不起作用,它只在第一张纸上放了“0”

Public Sub CountWorkSheets()
If Application.Sheets.Count = 2 Then
Sheets(1).Range("X5").Value = 1 And Sheets(2).Range("X5").Value = 2
End If
End Sub

I suggest go to ThisWorkbook and hook into the NewSheet event我建议 go 到ThisWorkbook并挂钩到NewSheet事件

scr

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Call AddSheetCounters
End Sub

Public Sub AddSheetCounters()
    Dim i As Long, n As Long
    
    n = Me.Sheets.Count
    
    For i = 1 To n
        Me.Sheets(i).Range("X5").Value = "Sheet " & i
        Me.Sheets(i).Range("X6").Value = "of " & n
    Next i

End Sub

Edit the text for ranges X5 and X6 as you need.根据需要编辑范围X5X6的文本。 Every time a new sheet is added the macro is run.每次添加新工作表时都会运行宏。 Unfortunately when a sheet is deleted, it won't and things will get out of sync.不幸的是,当工作表被删除时,它不会被删除,并且事情会变得不同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM