Simple Copy/Paste VBA is copying the same data multiple times :(

amandabstewart

New Member
Joined
Aug 4, 2014
Messages
45
this works, BUT, when I run the macro again, it copies old data as new at the bottom. it's ok for it to overwright content in A2 on the destination page, but it takes the data like it is new and I get repeat data in the destination table.
any ideas? trying to delete either of the set pastecell parts causes the macro to die :(



Option Explicit

Sub CopyP2()

Dim PatientCol As Range
Dim Patient As Range
Dim PasteCell As Range

Set PatientCol = Sheet1.Range("A2:A99")

For Each Patient In PatientCol


If Sheet4.Range("A2") = "" Then

Set PasteCell = Sheet4.Range("A2")
Else

Set PasteCell = Sheet4.Range("A1").End(xlDown).Offset(1, 0)
End If

If Patient = "P2" Then Patient.EntireRow.Copy PasteCell

Next Patient

End Sub
 
1633541851833.png
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
How about
VBA Code:
Sub amandabstewart()
   Sheet4.UsedRange.Offset(1).ClearContents
   With Sheet1
      .Range("a1").AutoFilter 1, "P2"
      .AutoFilter.Range.EntireRow.Copy Sheet4.Range("a2")
      .AutoFilterMode = False
   End With
End Sub
This will clear sheet4 & then copy the data over.
 
Upvote 0
How about
VBA Code:
Sub amandabstewart()
   Sheet4.UsedRange.Offset(1).ClearContents
   With Sheet1
      .Range("a1").AutoFilter 1, "P2"
      .AutoFilter.Range.EntireRow.Copy Sheet4.Range("a2")
      .AutoFilterMode = False
   End With
End Sub
This will clear sheet4 & then copy the data over.
this works but copies the headers over--is there a way to leave row 1 on both sheets as-is and only take the data in rows below?
 
Upvote 0
this works but copies the headers over
Oops, forgot to add the offset
VBA Code:
Sub amandabstewart()
   Sheet4.UsedRange.Offset(1).ClearContents
   With Sheet1
      .Range("a1").AutoFilter 1, "P2"
      .AutoFilter.Range.Offset(1).EntireRow.Copy Sheet4.Range("a2")
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Well my script searches column A for Exactly "P2"
I test my scripts. Are you running the script from the sheet with the values in column A?
yes--running exactly as you have listed and with P2 as data in column A
I must be doing something wrong....i promise it is NOT you
Well my script copies the data to sheet(2) you never mentioned sheet named P2
it is listed as sheet2 (P2) on the module list...
 
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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