gpt4 book ai didi

excel - 复制工作表

转载 作者:行者123 更新时间:2023-12-04 20:26:45 29 4
gpt4 key购买 nike

我正在尝试设置一个电子表格,每天将以前的工作表复制到一个新标签中。

我已经将它设置为创建一个新的工作表并每天重命名它,但我无法弄清楚重复方面。

Sub AddDayWkst()
Dim ws As Worksheet
Dim strName As String
Dim bCheck As Boolean

On Error Resume Next
strName = Format(Date, "mm-dd-yy")
bCheck = Len(Sheets(strName).Name) > 0

If bCheck = False Then
Set ws = Worksheets.Add(Before:=Sheets(1))
ws.Name = strName
End If

End Sub

我需要复制前一天的工作表并将其粘贴到新的工作表中。

最佳答案

Sub AddDayWkst()

Dim ws As Worksheet
Dim strNewName As String, strOldName As String
Dim bValid As Boolean

strNewName = Format(Date, "mm-dd-yy")
strOldName = Format(Date - 1, "mm-dd-yy")

bValid = WorksheetExists(strOldName)

If bValid Then

Set ws = Sheets(strOldName)
ws.Copy before:=Worksheets(1)
Worksheets(1).Name = strNewName

End If

End Sub

Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean

Dim sht As Worksheet

If wb Is Nothing Then Set wb = ThisWorkbook

On Error Resume Next

Set sht = wb.Sheets(shtName)
On Error GoTo 0

WorksheetExists = Not sht Is Nothing

End Function

关于excel - 复制工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718924/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com