将粘贴范围从一个工作表复制到另一个工作表

时间:2019-12-17 07:20:20

标签: excel vba copy range paste

我尝试将代码从N3:Y34复制到新工作表“计算所得的值”中的A1,但没有用。我尝试过.Paste.PasteSpecial,但没有任何变化。

It only shows this on the new sheet

Sheets.Add.Name = "Calculated Values"

'Copy the data
ActiveSheet.Range("N3:Y34").Copy
'Activate the destination worksheet
Sheets("Calculated Values").Activate
'Select the target range
Range("A1").Select
'Paste in the target destination
Sheets("Calculated Values").PasteSpecial

Application.CutCopyMode = False

1 个答案:

答案 0 :(得分:0)

您可以修改以下内容并尝试:

Option Explicit

Sub test()

    'Check if sheet excist
    If Sheet_Exists("Calculated Values") = False Then

        Worksheets.Add().Name = "Calculated Values"

    End If

    With ThisWorkbook
        'Copy with formats
        .Worksheets("Sheet1").Range("N3:Y34").Copy .Worksheets("Calculated Values").Range("A1")
        'Copy only values
        .Worksheets("Sheet1").Range("N3:Y34").Copy
        .Worksheets("Calculated Values").Range("A1").PasteSpecial xlPasteValues
    End With

End Sub

Function Sheet_Exists(WorkSheet_Name As String) As Boolean

    Dim Work_sheet As Worksheet

    Sheet_Exists = False

    For Each Work_sheet In ThisWorkbook.Worksheets

        If Work_sheet.Name = WorkSheet_Name Then
            Sheet_Exists = True
        End If

    Next

End Function