Cut and paste vba code

caos88

Board Regular
Joined
Mar 12, 2020
Messages
66
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone,

i have datea to be copied and paste to another sheet according to the value. However, i made the code but it will overwrite the old data if i continue on another sheet. How to avoid it ?

Sub Copy()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet


' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("sheet1")
Set Target = ActiveWorkbook.Worksheets("sheet2")

j = 1 ' Start copying to row 1 in target sheet
For Each c In Source.Range("I1:I1000") ' Do 1000 rows
If c = "Closed" Then
Source.Rows(c.Row).Copy Target.Rows(j)
Source.Rows(c.Row).Cut Target.Rows(j)
j = j + 1

End If
Next c
End Sub
 
Are you saying that you want the data on sheet cleared, before running the macro?
If not, then my code should work as it doesn't overwrite anything, just adds it below the last lot of data
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
alternatively using your code you provided just change j to identify the last row
see if this works (i havent tested it)

VBA Code:
Sub Copy()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet


    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("sheet1")
    Set Target = ActiveWorkbook.Worksheets("sheet2")

    j = Target.Range("A" & Rows.Count).End(xlUp).Row + 1     ' Start copying 1 down from the last row on sheet
    For Each c In Source.Range("I1:I1000")   ' Do 1000 rows
        If c = "Closed" Then
           Source.Rows(c.Row).Cut Target.Rows(j)
           j = j + 1
     
        End If
    Next c
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top