Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy data to another worksheet
#1
Example:

Sub CopyDataToAnotherSheet()
    Dim sourceWs As Worksheet
    Dim targetWs As Worksheet
    Dim lastRowSource As Long
    Dim lastRowTarget As Long

    'Set the source worksheet where the data is located
    Set sourceWs = ThisWorkbook.Sheets("SourceSheet")

    'Set the target worksheet where the data will be copied
    Set targetWs = ThisWorkbook.Sheets("TargetSheet")

    'Find the last row with data in the source worksheet
    lastRowSource = sourceWs.Cells(sourceWs.Rows.Count, "A").End(xlUp).Row

    'Find the last row with data in the target worksheet
    lastRowTarget = targetWs.Cells(targetWs.Rows.Count, "A").End(xlUp).Row

    'Copy the data from the source worksheet to the target worksheet
    sourceWs.Range("A1:A" & lastRowSource).Copy targetWs.Range("A" & lastRowTarget + 1)
End Sub

Explanation:

The code defines a subroutine named CopyDataToAnotherSheet.
It declares variables for the source worksheet (sourceWs), the target worksheet (targetWs), and the last row with data in each worksheet (lastRowSource and lastRowTarget).
Set sourceWs = ThisWorkbook.Sheets("SourceSheet") specifies the source worksheet ("SourceSheet") where the data is located.
Set targetWs = ThisWorkbook.Sheets("TargetSheet") specifies the target worksheet ("TargetSheet") where the data will be copied.
The lastRowSource variable is determined using sourceWs.Cells(sourceWs.Rows.Count, "A").End(xlUp).Row, which finds the last row with data in column A of the source worksheet.
The lastRowTarget variable is determined using targetWs.Cells(targetWs.Rows.Count, "A").End(xlUp).Row, which finds the last row with data in column A of the target worksheet.
The data from column A of the source worksheet (from A1 to the last row with data) is copied to the target worksheet starting from the next empty row in column A.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate data in columns Qomplainerz 0 344 07-27-2023, 11:01 AM
Last Post: Qomplainerz
  Create a pivot table based on data in a worksheet with VBA Qomplainerz 0 413 07-27-2023, 09:48 AM
Last Post: Qomplainerz
  Filter data in a worksheet Qomplainerz 0 380 07-27-2023, 09:46 AM
Last Post: Qomplainerz
  Create a chart from data with VBA Qomplainerz 0 379 07-27-2023, 09:44 AM
Last Post: Qomplainerz
  Sort data in a range with VBA Qomplainerz 0 366 07-27-2023, 09:43 AM
Last Post: Qomplainerz
  Insert a new row and fill it with data in VBA Qomplainerz 0 343 07-27-2023, 09:11 AM
Last Post: Qomplainerz
  Add data validation to a cell with VBA Qomplainerz 0 353 07-27-2023, 09:10 AM
Last Post: Qomplainerz
  Copy and paste values with VBA Qomplainerz 0 346 07-27-2023, 09:08 AM
Last Post: Qomplainerz
  Protect and unprotect worksheet with VBA Qomplainerz 0 301 07-27-2023, 08:20 AM
Last Post: Qomplainerz
  Create a new worksheet and rename it with VBA Qomplainerz 0 306 07-27-2023, 08:14 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 6 Guest(s)