Google News
logo
Management Information System (MIS) - Interview Questions
Write a VBA program to copy data from a worksheet to another?
Here's a simple VBA program that copies data from one worksheet to another in the same workbook :
Sub CopyData()
    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim sourceRange As Range
    Dim targetRange As Range
    
    ' Set the source and target worksheets
    Set sourceSheet = ThisWorkbook.Worksheets("SourceSheet") ' Change "SourceSheet" to the name of your source worksheet
    Set targetSheet = ThisWorkbook.Worksheets("TargetSheet") ' Change "TargetSheet" to the name of your target worksheet
    
    ' Define the source range (assuming data starts from cell A1)
    Set sourceRange = sourceSheet.Range("A1").CurrentRegion ' Change "A1" to the starting cell of your data range
    
    ' Define the target range (start copying data to cell A1)
    Set targetRange = targetSheet.Range("A1") ' Change "A1" to the starting cell where you want to paste your data
    
    ' Copy data from source range to target range
    sourceRange.Copy targetRange
    
    ' Clear clipboard
    Application.CutCopyMode = False
    
    MsgBox "Data copied successfully from '" & sourceSheet.Name & "' to '" & targetSheet.Name & "'.", vbInformation
End Sub?

Before running the code, make sure to replace "SourceSheet" and "TargetSheet" with the names of your source and target worksheets, respectively. Also, adjust the starting cell of your data range as needed.

This code copies data from the source worksheet starting from cell A1 and pastes it into the target worksheet starting from cell A1. You can modify the ranges as per your specific requirements.
Advertisement