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

amandabstewart

New Member
Joined
Aug 4, 2014
Messages
40
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
 

amandabstewart

New Member
Joined
Aug 4, 2014
Messages
40
1633541851833.png
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

amandabstewart

New Member
Joined
Aug 4, 2014
Messages
40
this is source data. If P2 is found i want to copy the entire row to P2's own sheet

Jp, P1, P2 are all in column A
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,957
Office Version
  1. 2013
Platform
  1. Windows
Do you mean contains "P2"

Like my name is P2
Or do you mean just "P2"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,994
Office Version
  1. 365
Platform
  1. Windows
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.
 

amandabstewart

New Member
Joined
Aug 4, 2014
Messages
40

ADVERTISEMENT

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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,994
Office Version
  1. 365
Platform
  1. Windows
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
 

amandabstewart

New Member
Joined
Aug 4, 2014
Messages
40
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...
 

Forum statistics

Threads
1,144,422
Messages
5,724,221
Members
422,544
Latest member
joanwalter

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
Top