Excel VBA用来自最后一张表的数据创建一个新表

时间:2018-07-28 22:37:34

标签: excel vba excel-vba

对,这对我来说是很大的。我正在查看一个根据当前工作表月份创建新工作表的按钮。 我目前拥有创建新工作表并为其指定下个月名称的代码,但是,现在我需要将格式,刻度框和按钮从最后一个工作表复制到新工作表中。我确定自己可以自己解决从特定单元格区域复制数据的问题了。

我有以下用于月份命名的代码,它可以按预期完美地工作,任何人都知道如何实现对此的复制:

Private Sub CreateNewMonth_Click()
Dim wSheet As Worksheet
Dim strName As String
Dim lMonth As Long
Dim lIndex As Long
Dim WrkShtSrc As Worksheet
Dim WrkShtTgt As Worksheet

    For Each wSheet In Worksheets
        Select Case wSheet.Name
            Case "Jan", "January"
                lMonth = 2
                lIndex = wSheet.Index
            Case "Feb", "Febuary"
                If lMonth < 3 Then lMonth = 3
                lIndex = wSheet.Index
            Case "Mar", "March"
                If lMonth < 4 Then lMonth = 4
                lIndex = wSheet.Index
            Case "Apr", "April"
                If lMonth < 5 Then lMonth = 5
                lIndex = wSheet.Index
            Case "May"
                If lMonth < 6 Then lMonth = 6
                lIndex = wSheet.Index
            Case "Jun", "June"
                If lMonth < 7 Then lMonth = 7
                lIndex = wSheet.Index
            Case "Jul", "July"
                If lMonth < 8 Then lMonth = 8
                lIndex = wSheet.Index
            Case "August", "August"
                If lMonth < 9 Then lMonth = 9
                lIndex = wSheet.Index
            Case "September", "Septemeber"
                If lMonth < 10 Then lMonth = 10
                lIndex = wSheet.Index
            Case "October", "October"
                If lMonth < 11 Then lMonth = 11
                lIndex = wSheet.Index
            Case "November", "November"
                If lMonth < 12 Then lMonth = 12
                lIndex = wSheet.Index
            Case "December", "December"
                If lMonth < 12 Then lMonth = 12
                lIndex = wSheet.Index
            Case Else
                If lMonth = 0 Then
                  lMonth = 1
                  lIndex = wSheet.Index
                End If
            End Select
        Next wSheet

If lMonth <> 0 And lMonth < 13 Then
On Error Resume Next
Worksheets.Add After:=Worksheets(lIndex)
ActiveSheet.Name = Format(DateSerial(Year(Date), lMonth, 1), "mmmm")
On Error GoTo 0
End If

End Sub

谢谢您的帮助

1 个答案:

答案 0 :(得分:1)

使用

worksheets(lIndex).Copy After:=Worksheets(lIndex)

复制worksheets(lIndex)旁边的worksheets(lIndex)