Copy Range to new sheet but checking first for duplicates

trillium

Board Regular
Joined
Aug 9, 2010
Messages
63
Hi! I'm trying to modify an existing macro... and am running into problems.

I want to copy a range to a new sheet, however when we get to the new sheet, I want to check first if an entry with the same Unique Identifier already exists. If YES, then copy over it. If NO, then copy into the next available row.

I can't seem to get my code to stop looping.... not sure why.. it seem to has something to do with the ActiveCell.. I thought that was on the History sheet but I'm suspecting its not..

Sheet with info to copy = PR sheet
AuditNum has unique identifier in AJ4 of PR sheet

Destination Sheet = History sheet
Where to start checking for duplicates = Between A4 and last row

Thank you in advance for your great advice! I always learn so much!

Code:
Sub CopyPRData()
'Copies values from PR form to a History Tab so history of edits will not be lost

    Dim PRLastCell As String 'on PRHistory Sheet
    Dim PRTrackingType As String 'to match the AuditNum from PRSheet
    Dim PRAuditNum As String 'From PR Sheet
    
PRLastCell = Sheets("PRHistory").Range("A655").End(xlUp).Address
    Sheets("PR").Activate
    Range("AJ4").Select
    
    Application.ScreenUpdating = False
    
    Do Until ActiveCell.Address = PRLastCell          
            PRTrackingType = ActiveCell.Value
            PRAuditNum = Sheets("PR").Range("AJ4").Value
            Range("AJ4:BS4").Copy
                                 
                   Sheets("PRHistory").Activate
                        
                        'Check for duplicate audit number
                        'Range("A4").Select
                        Do Until ActiveCell.Value = PRAuditNum Or ActiveCell.Value = ""
                        ActiveCell.Select
                          Loop
                        ActiveCell.PasteSpecial xlPasteValues
                
 
            Application.CutCopyMode = False
            Range("A1").Select
            
    Loop
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Dryver14

Well-known Member
Joined
Mar 22, 2010
Messages
2,396
if you are look at 2 values before making a decision on where to paste you are probably best not looping at all i would suggest select case based on range("AJ4).value
 

Dryver14

Well-known Member
Joined
Mar 22, 2010
Messages
2,396
I thinks so


case value this.......paste here
case value that .....paste there
case else ....do nothing
 

trillium

Board Regular
Joined
Aug 9, 2010
Messages
63
Hey! I can copy it over, but it won't paste on top if it finds the same unique identifier... just pastes under it???

Code:
Sub CopyPRData()
Dim intAuditPR As String
Dim intPRData As String
Dim PRLastCell As String 'on PRHistory Sheet
PRLastCell = Sheets("PRHistory").Range("A655").End(xlUp).Address
'get variables
intAuditPR = Sheets("PeerReview").Range("AJ4").Value
'intPRData = Sheets("PeerReview").Range("AJ4:FC4").Value
Select Case intAuditPR
Case Is = PRLastCell
        Sheets("PeerReview").Select
        Range("inPRData").Select
        Selection.Copy
        
        Sheets("PRHistory").Select
        Range(PRLastCell).Offset(-1, 0).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        Sheets("PRHistory").Range("A1").Select
    
Case Is <> PRLastCell
        Sheets("PeerReview").Select
        Range("AJ4:FC4").Select
        Selection.Copy
        Sheets("PRHistory").Select
        Range(PRLastCell).Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        
        Sheets("PRHistory").Range("A1").Select
End Select
End Sub
[\code]
 

Forum statistics

Threads
1,141,016
Messages
5,703,751
Members
421,312
Latest member
Mooncake1

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