欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vba 拷贝一个sheet页的内容到另一个sheet中

程序员文章站 2022-07-13 15:30:27
...

 模板:templateName,目标sheet:sheetName

具体代码如下:

Sub SheetCopy(sheetName As String, templateName As String)
'
' SheetCopy Macro
'

'
    Call AddWorksheetAfterLast(sheetName)
    Worksheets(templateName).Select
    Range("A:B").Select
    Selection.Copy
    Worksheets(sheetName).Select
    Range("A:B").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    'ActiveWorkbook.Save
    
End Sub

Private Sub AddWorksheetAfterLast(sheetName As String)
'
' AddWorksheetAfterLast Macro
'

'
    If (SheetExists(sheetName)) Then
        'Empty the contents of sheet
        Worksheets(sheetName).UsedRange.ClearContents
    Else
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sheetName
    End If
End Sub

Private Function SheetExists(sheetName As String) As Boolean
'
' SheetExists
'

'
    Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name <> sheetName Then
            SheetExists = False
        Else
            SheetExists = True
            Exit For
        End If
    Next
End Function