QP School

Full Version: Copy data from one workbook to another with VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub CopyData()
    Dim sourceSheet As Worksheet
    Dim destinationSheet As Worksheet

    'Set the source and destination sheets
    Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
    Set destinationSheet = ThisWorkbook.Sheets("Sheet2")

    'Copy data from source to destination
    sourceSheet.Range("A1:C10").Copy destinationSheet.Range("A1")
End Sub

Explanation:

This code defines a subroutine named CopyData. 
It declares two variables sourceSheet and destinationSheet, representing the worksheets from which you want to copy data and where you want to paste it. 
The Set statement is used to specify the source and destination sheets (Sheet1 and Sheet2) within the same workbook. 
The Copy method is then used to copy the data from cell range A1:C10 on sourceSheet to cell range A1 on destinationSheet.

The Copy method is used to copy a range of cells from one worksheet to another. 
The method is invoked on the source range (sourceSheet.Range("A1:C10")) and specifies the destination range (destinationSheet.Range("A1")) as the place where the data will be pasted.