Results 1 to 1 of 1

Copy a row, check table for duplicate, give option to overwrite duplicate before pasting

Threaded View

  1. #1
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    312

    Copy a row, check table for duplicate, give option to overwrite duplicate before pasting

    Hi
    I have a workbook (MASTER) that copies a single row of information on contact names and addresses from other workbooks (Slave1, Slave2, etc) and pastes the information into a worksheet (ALL DATA) in MASTER.

    Before pasting into MASTER, I want the process to check if the contact information is already in ALLDATA / MASTER. If it is, I need the option of overwriting the existing data, (there may be a change to address or some such), whatever row it's in, or paste the information into a new line if it's a new contact and doesn't cause a duplicate.

    So far I've found a process that identifies a duplicate in a single cell in a column, but then asks for you to type in new information to overwrite the information to be pasted, rather than paste over the existing information.

       Private Sub Worksheet_Change(ByVal Target As Range)
    'Check for duplicates in the table
    
            Dim Msg2 As String, Ans2 As Variant
                Dim FindRowNumber As Long
                    Dim rng As Range, r As Range, Msg1 As String, x As Range, temp, check
        Set rng = Intersect(Columns(1), Target)
        If Not rng Is Nothing Then
            Application.EnableEvents = False
            For Each r In rng
                If Not IsEmpty(r.Value) Then
                    If Application.CountIf(Columns(1), r.Value) > 1 Then
                          Msg1 = Msg1 & r.Value
                        If x Is Nothing Then
                            r.Activate
                            Set x = r
                        Else
                            Set x = Union(x, r)
                        End If
                    End If
                End If
            Next
            If Len(Msg1) Then
            
                MsgBox "An Entry already exists for " & Msg1
                x.Select
                For Each r In x
                    r.Activate
                    Do
                       Msg2 = "Would you like to Overwrite the current entry for " & _
                        r.Value
                        Ans = MsgBox(Msg2, vbYesNo)
    
     'NEED TO PASTE OVER THE CURRENT ENTRY HERE.........                   
                     
    temp = Application.InputBox(Msg2 & _
                        r.Value, "Invalid ; " & IIf(Len(temp), temp, r.Value))
                       check = Application.CountIf(Columns(1), temp)
                       
                    Loop Until (check = 0) * (temp <> "") * (temp <> False)
                    r.Value = temp
                    temp = ""
                Next
            End If
            Set rng = Nothing
            Set x = Nothing
            Application.EnableEvents = True
        End If
    End Sub
    I've attached a mock workbook in which I copy the list on sheet 2 into column A on sheet 1 to show the above code working, but now need to expand the code to identify the row the duplicate info is in and overwrite it with the info to be pasted, or paste the incoming info as a new line at the end of the list.

    Thanks in advance!!

    Frankie
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Overwrite value instead of skip duplicate Worksheet_Change_Event
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-18-2017, 11:59 AM
  2. Replies: 4
    Last Post: 08-21-2015, 03:59 PM
  3. Overwrite or delete option if some datas are duplicate and add another row if not dupe.
    By useyourname in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2013, 12:41 AM
  4. Check duplicate then compare latest version then give a value
    By henkdevreisch in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-06-2013, 07:23 AM
  5. check duplicate records within same table
    By cthorn112 in forum Access Tables & Databases
    Replies: 0
    Last Post: 06-21-2012, 11:26 AM
  6. Search column a for duplicate data and copy entire row to duplicate sheet
    By crazyAMP in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2012, 08:21 PM
  7. edit macro that is pasting info to check column for duplicate info
    By roth_nj in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2009, 05:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1