如何计算表名称作为整数?

我有很多表单(大约1000),名字是整数,如:1 2 3 5 7 10等。他们是上升的,但不一致,就像我写的。 我有vba代码,创build新的工作表,从inputbox的numbername,在activesheet(我激活工作表3,运行代码,input框中input4,并创build工作表4后3)。 我需要的是解决scheme如何(示例):在sheetnumber 3之后创build名称为4的新工作表,而不必在工作表3上。

下面的代码遍历所有表单。 当它到达一个更大的数字时,它会在它之前插入新的表格。

Public Sub Test() AddSheetWithNumber shNum:=4 End Sub Public Sub AddSheetWithNumber(shNum As Long) With ThisWorkbook Dim sh As Worksheet For Each sh In Worksheets ' Find first sheet with number greater than new sheet number If CLng(sh.Name) > shNum Then ' Add worksheet before sheet with larger number .Worksheets.Add before:=sh ActiveSheet.Name = CStr(shNum) Exit For End If Next End With End Sub 

在我看到你的例子之前,我想出了类似的解决scheme。 这也许是有用的:

 Sub New_Sheet() Dim ExistingSheet As Integer Dim NewSheet As Integer On Error GoTo 30 NewSheet = InputBox("Enter new sheet:", "NEW SHEET") For i = 3 To Sheets.Count - 3 'this is my work range ExistingSheet = Sheets(i).Name If ExistingSheet = NewSheet Then MsgBox ("That sheet already exist!") Sheets(i).Activate GoTo 30 Else On Error GoTo 10 'last 4 sheets have textual name like (subtotal, partners, etc) so error came up if I want to add sheet with bigest number(because it's comparing a textual name) If NewSheet > ExistingSheet Then 'error came up only in that case for now, so I make it place biggest sheet before textual one. GoTo 20 Else ActiveWorkbook.Sheets("Empty Card").Copy _ after:=Sheets(i - 1) 'Sheet with formulas and tables prepared for work ActiveSheet.Name = NewSheet ActiveSheet.Cells(2, 13) = ActiveSheet.Name Exit Sub End If End If 20 Next i 10 ActiveWorkbook.Sheets("Empty card").Copy _ after:=Sheets(i - 1) 'didn't know of "befor" command ActiveSheet.Name = NewSheet ActiveSheet.Cells(2, 13) = ActiveSheet.Name 30 Exit Sub End Sub