【问题标题】:VBA Excel add new sheet with number based on the previous sheet createdVBA Excel根据创建的上一个工作表添加带有编号的新工作表
【发布时间】:2021-02-10 17:33:37
【问题描述】:

我有一张名为“区域地图 1”的工作表。 我想创建一个按钮,它将为我添加名为“区域地图 2”的新工作表(复制“区域地图 1”)。 该按钮将仅添加一张纸。这意味着,如果我们需要创建更多工作表,它可以重复使用。但是,如果我使用此按钮一次,那么我在此名称下的最后一个现有工作表是“区域地图 2”。再次使用该按钮将导致错误“名称已被占用,请尝试其他名称”。

那么我应该在下面的代码中改进什么?

  Sub ConsecutiveNumberSheets()
  Dim ws As Worksheet
  Dim i As Long
  For i = 1 To Sheets.Count - (Sheets.Count - 1)
  With Sheets("Area Map 1")
  .Copy after:=ActiveSheet
  ActiveSheet.Name = "Area Map " & (i + 1)
  .Select
  End With
  Next i
  End Sub

我想要一些东西,它会检测到已经创建了带有递增数字的新工作表。我应该怎么做才能使我的代码基于现有工作表的最后一个数字?

【问题讨论】:

  • 如果您只想添加一张,为什么要循环浏览所有工作表?
  • 在您创建另一个副本之前可能已经删除了以前的副本吗?所以你可能有 map1 和 Map 3 但 Map 2 被删除了,然后你想要另一个副本 - 它应该是 Map 4 还是 Map 2 ?
  • @SJR 这是其中一种方式。我还不知道如何简单地添加一张纸所以我使用了循环。

标签: excel vba


【解决方案1】:

添加增量工作表

Option Explicit

Sub createIncrementedWorksheet()
    
    Const wsPattern As String = "Area Map "
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim arr() As Long: ReDim arr(1 To wb.Sheets.Count)
    Dim wsLen As Long: wsLen = Len(wsPattern)
    
    Dim sh As Object
    Dim cValue As Variant
    Dim shName As String
    Dim n As Long
    
    For Each sh In wb.Sheets
        shName = sh.Name
        If StrComp(Left(shName, wsLen), wsPattern, vbTextCompare) = 0 Then
            cValue = Right(shName, Len(shName) - wsLen)
            If IsNumeric(cValue) Then
                n = n + 1
                arr(n) = CLng(cValue)
            End If
        End If
    Next sh
    If n = 0 Then
        n = 1
    Else
        ' If you just want the number to be one greater then the greatest,
        ' you can use the one liner...
        'n = Application.Max(arr) + 1
        ' ... instead of the following before 'End If':
        ReDim Preserve arr(1 To n)
        For n = 1 To n
            If IsError(Application.Match(n, arr, 0)) Then
                Exit For
            End If
        Next n
    End If
            
    Set sh = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count))
    sh.Name = wsPattern & CStr(n)

End Sub

【讨论】:

  • 您知道如何不添加新工作表,而是复制现有工作表吗?他们结束了我的问题:stackoverflow.com/questions/66152671/…
  • @VBasic2008 为什么要将它添加到数组中?
  • 我有两个问题: 1. 假设"Area Map X" 是一个工作表,下面我将只使用X。如果例如你有1,2,3,你删除2,然后你有1,3。添加另一个时,应该是2 还是4? 2. 又是1,2,3。您究竟想复制(现有工作表!?)并增加什么? - 数组只帮助我找到下一个可用的数字。
【解决方案2】:

这应该可以满足您的需求。

Public Sub CreateSheet()
    Dim wb As Workbook: Set wb = ActiveWorkbook
    Dim ws As Worksheet
    Dim startName As String: startName = "Area Map "
    Dim counter As Integer: counter = 1
    
    For Each ws In wb.Sheets                      
        If Left(ws.Name, Len(startName)) = startName Then
            counter = counter + 1
        End If            
    Next ws
    
    Set ws = wb.Sheets.Add
    startName = startName & counter
    ws.Name = startName
End Sub

【讨论】:

  • 调试器说:startName 未定义
  • 确定已排序。它是 Dim startNmae 而不是 startName
  • 我已经修好了。对此感到抱歉。
  • 谢谢。你能告诉我为了复制现有工作表而不是创建新工作表,我应该在此代码中进行哪些更改?他们已经关闭了我对此的新查询:stackoverflow.com/questions/66152671/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多