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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi & welcome to MrExcel.
Is this what you want?
VBA Code:
Sub caos()
   With Sheets("Sheet1")
      .Range("A1:I1").AutoFilter 9, "Closed"
      With .AutoFilter.Range.Offset(1).EntireRow
         .Copy Sheets("Sheet2").Range("I" & Rows.Count).End(xlUp).Offset(1, -8)
         .Delete
      End With
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Hi,

thank you. not really. This code will remove the rows only in A1:I1. I would like to cut and copy rows with a certain value (closed) to another sheet. and i don't want to overwrite items on the destination sheet.
 
Upvote 0
Have you actually tested the code that Fluff has posted?
 
Upvote 0
yes, you are aright. it works but, i actually have to move only the rows when "completed" is triggered in the last column I and cut an paste to the other sheet......doing it on different sheet avoiding overwriting at the end
 
Upvote 0
The code that i posted earlier works as well......but it overwrites if i run it different times from different sheets.

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
 
Upvote 0
Do you want to run the code on the active sheet & move the rows to Sheet2?
 
Upvote 0
Yes, i want to run it on sheet 1 and cut and copy to sheet 2 after i choose the word completed.....on column I
 
Upvote 0
if i have 2 rows with completed value and i run the code it will be ok. If i do it twice, the second time will overwrite.
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

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