复制 Excel 工作表并粘贴为同一工作簿中的另一个选项卡作为文档目的值的最有效的 vba 代码是什么

问题描述 投票:0回答:1

我正在寻找一种解决方案,以有效地复制 Excel 选项卡并将其作为值粘贴到同一工作簿中以用于文档目的。下面是我可以提出的当前代码,但我发现它需要一分钟多的时间才能完成,并希望有一个更快的解决方案。任何建议都会有帮助,并提前感谢您。

Sub copy_paste()

    Application.screenupdating=false
    Application.calculation=xlcalculationmanual
    Application.cutcopymode=false
    
    ‘Copy sheet and paste as second tab and use A1 as tab title’
    Dim wks As worksheet  
    Set wks=activesheet
    Wks.copy before:= shtDeskEP (2nd excel tab)
    
        If wks.range(“a1”).value<>”” then
            On error resume next
            Activesheet.name=wks.range(“a1”).value
    
        End if
    Application.screenupdating=false
    Application.calculation=xlcalculationautomatic
    Application.cutcopymode=true

End sub
excel vba duplicates copy
1个回答
0
投票

你可以使用类似的东西:

Sub DuplicateSheetsAsValues()
    Dim ws As Worksheet
    Dim newWs As Worksheet
    Dim wsName As String
    
    ' Turn off screen updating and automatic calculations to improve speed
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    For Each ws In ThisWorkbook.Worksheets
        ' Create a duplicate sheet
        ws.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        
        ' Reference the new sheet
        Set newWs = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        
        ' Rename the new sheet with a suffix to avoid duplicate names
        newWs.Name = ws.Name & "_Copy"
        
        ' Paste values only
        newWs.UsedRange.Value = newWs.UsedRange.Value
    Next ws

    ' Restore settings
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.