Copying range and pasting into new workbook

后端 未结 2 616
死守一世寂寞
死守一世寂寞 2021-01-03 01:26

This should be really simple, but I\'ve been trawling forums and SO answers for hours to find the answer with no luck, so am (reluctantly) creating a question of my own.

2条回答
  •  长情又很酷
    2021-01-03 01:54

    Is this what you are trying? I have commented the code so that you shouldn't have any problem understanding what the code does.

    Option Explicit
    
    Sub Sample()
        Dim wbI As Workbook, wbO As Workbook
        Dim wsI As Worksheet, wsO As Worksheet
    
        '~~> Source/Input Workbook
        Set wbI = ThisWorkbook
        '~~> Set the relevant sheet from where you want to copy
        Set wsI = wbI.Sheets("Sheet1")
    
        '~~> Destination/Output Workbook
        Set wbO = Workbooks.Add
    
        With wbO
            '~~> Set the relevant sheet to where you want to paste
            Set wsO = wbO.Sheets("Sheet1")
    
            '~~>. Save the file
            .SaveAs Filename:="C:\Book2.xls", FileFormat:=56
    
            '~~> Copy the range
            wsI.Range("A1:B10").Copy
    
            '~~> Paste it in say Cell A1. Change as applicable
            wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        End With
    End Sub
    

提交回复
热议问题