excel - 添加具有连续名称的工作表在第 10 个工作表之后停止递增

标签 excel vba

在寻找使用 VBA 顺序添加工作表的方法时,我遇到了以下代码。这段代码效果很好,并且完全符合我的需要,直到它到达第 10 个工作表。一旦它到达第 10 页(即 "Combined-10"),它会在尝试前进到第 11 页等时引发错误。我对 VBA 相当陌生,不知道如何纠正这个问题。我只是需要帮助解决此问题,以便工作表在达到第 10 表后继续前进到下一个连续工作表。任何帮助将不胜感激!
链接到原始代码:
添加具有连续名称的工作表
Adding Sheets With Sequential Names

Option Explicit

Sub GetAvailableSheeName()

Dim sht As Worksheet
Dim temp_sht
Dim sht_name, last_sht As String
Dim shtNumber
Dim temp_counter, loop_i, counter, num As Integer

Const Available_sht As String = "Combined-"

temp_counter = 0
For Each sht In ThisWorkbook.Worksheets

    If LCase(Left(sht.name, Len(Available_sht))) = LCase(Available_sht) Then

        shtNumber = Split(sht.name, "-")(1)

        If IsNumeric(shtNumber) Then
            If shtNumber > temp_counter Then
                temp_counter = shtNumber
                last_sht = sht.name
            End If

        Else
            sht_name = sht.name

        End If

    Else
            sht_name = sht.name
    End If

Next sht

If temp_counter = 0 Then

   ThisWorkbook.Sheets.Add(After:=Sheets(sht_name)).name = "Combined-1"
Else

   ThisWorkbook.Sheets.Add(After:=Sheets(last_sht)).name = "Combined-" & temp_counter + 1

    For loop_i = 1 To temp_counter + 1

        For Each sht In ThisWorkbook.Worksheets
             counter = 0
             If LCase("Combined-") & loop_i = LCase(sht.name) Then

               counter = 1
               Exit For
             End If

        Next sht

         If counter = 0 Then
            If loop_i = 1 Then
              ThisWorkbook.Sheets.Add(Before:=Sheets(1)).name = "Combined-" & loop_i
            Else
              num = loop_i - 1
              ThisWorkbook.Sheets.Add(After:=Sheets("Combined-" & num)).name = "Combined-" & loop_i
            End If

         End If

        Next loop_i
End If

End Sub

最佳答案

添加具有连续名称的工作表
紧凑型

Sub GetAvailableSheetName()
    On Error GoTo ClearError
    
    Dim ws As Worksheet
    Dim n As Long
    Dim wsName As String
    
    Do
        n = n + 1
        wsName = "Combined-" & n
        Set ws = ThisWorkbook.Worksheets(wsName)
    Loop
        
WorksheetNaming:
    On Error Resume Next
    ThisWorkbook.Worksheets .Add(After:=ThisWorkbook _
        .Sheets(ThisWorkbook.Sheets.Count)).Name = wsName
    On Error GoTo 0
    
    Exit Sub

ClearError:
    Resume WorksheetNaming
End Sub
争论
Sub AddSequentialSheetNameTEST()
    AddSequentialSheetName ThisWorkbook, "Combined-"
    MsgBox "Added the worksheet '" & ActiveSheet.Name, vbInformation
End Sub


Sub AddSequentialSheetName( _
        ByVal wb As Workbook, _
        Optional ByVal Prefix As String = "Sheet", _
        Optional ByVal Suffix As String = "")
    On Error GoTo ClearError
    
    Dim ws As Worksheet
    Dim n As Long
    Dim wsName As String
    
    Do
        n = n + 1
        wsName = Prefix & n & Suffix
        Set ws = wb.Worksheets(wsName)
    Loop
        
WorksheetNaming:
    On Error Resume Next
    wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = wsName
    On Error GoTo 0
    
    Exit Sub

ClearError:
    Resume WorksheetNaming
End Sub

关于excel - 添加具有连续名称的工作表在第 10 个工作表之后停止递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71446282/

相关文章:

Excel VBA 2016 - 试图找到复选框的名称,无法获取值属性错误

excel - 计算动态数组/范围内的小计

excel - 仅粘贴值(案例)

excel - 如何在Access数据库中使用左连接从Excel表中进行选择 - EXCEL VBA

python - 如何使用 Openpyxl 分割 Excel 屏幕?

python - 如何在不加载整个文件的情况下从 XLS 文件中获取工作表名称?

excel - 在 VBA 中将数组另存为制表符分隔的文本文件

excel - 带有符号 Excel 的设计下拉菜单

vba - Excel VBA - 以 0 开头的字符串

vba - 使用 vba 在范围内的依赖单元格上添加公式