+ Reply to Thread
Results 1 to 4 of 4

Check for Duplications when transferring data from One Workbook to Master Workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-19-2012
    Location
    Woodbridge, Virginia
    MS-Off Ver
    Excel 2010
    Posts
    111

    Check for Duplications when transferring data from One Workbook to Master Workbook

    Hello,

    I need your help! I wanted to see if there was a way to check for duplicates before transferring data from one workbook to my master workbook. Is there a way to scan the rows of the master workbook and see if the data being transferred over is a duplicate? Here is the code I am running to copy the data to my master. Please let me know. Any suggestions will be greatly appreciated!

    Sub copyFormToMaster()
    
    
    Application.ScreenUpdating = False
    
    Workbooks.Open FileName:=ThisWorkbook.Path & "\CRForm.xlsm"
    
    
    Windows("CRForm.xlsm").Activate
    Unload requestForm
            
            dataB = Sheets("Database").Range("B2")
            dataC = Sheets("Database").Range("C2")
            dataD = Sheets("Database").Range("D2")
            dataE = Sheets("Database").Range("E2")
            dataF = Sheets("Database").Range("F2")
    
            
    Windows("ChangeRequestMaster.xlsm").Activate
      
    Dim nextRow As Long
    Dim dataSheet As Worksheet
    Set dataSheet = ThisWorkbook.Worksheets("Database")
    
    With dataSheet
    nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
            With .Cells(nextRow, "A")
                .Value = "ID" & nextRow
            End With
            .Cells(nextRow, "B").Value = dataB
            .Cells(nextRow, "C").Value = dataC
            .Cells(nextRow, "D").Value = dataD
            .Cells(nextRow, "E").Value = dataE
            .Cells(nextRow, "F").Value = dataF
     
            
    End With
    
    Windows("CRForm.xlsm").Activate
    ActiveWindow.Close
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by s2jrchoi; 02-16-2014 at 09:15 AM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Check for Duplications when transferring data from One Workbook to Master Workbook

    Maybe:

    Sub copyFormToMaster(): Dim P, Q
    
    
    Application.ScreenUpdating = False
    
    Workbooks.Open FileName:=ThisWorkbook.Path & "\CRForm.xlsm"
    
    
    Windows("CRForm.xlsm").Activate
    Unload requestForm
            
            dataB = Sheets("Database").Range("B2")
            dataC = Sheets("Database").Range("C2")
            dataD = Sheets("Database").Range("D2")
            dataE = Sheets("Database").Range("E2")
            dataF = Sheets("Database").Range("F2")
            P = Sheets("Database").Range("B2:F2")
            
    Windows("ChangeRequestMaster.xlsm").Activate
      
    Dim nextRow As Long, i As Long, j As Long, r As Long, c As Long
    Dim dataSheet As Worksheet
    Set dataSheet = ThisWorkbook.Worksheets("Database")
    
    With dataSheet
    nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
                    r = nextRow - 1: c = 6
           
                    For i = 2 To r
                Q = .Range(.Cells(i, 2), .Cells(i, 6))
                If P(1, 2) = Q(i, 2) And _
                    P(1, 3) = Q(i, 3) And _
                        P(1, 4) = Q(i, 4) And _
                            P(1, 5) = Q(i, 5) And _
                                P(1, 6) = Q(i, 6) Then
                                    GoTo ExitSub: End If
                     Next i
    
                    
            .Cells(nextRow, "A").Value = "ID" & nextRow
            .Cells(nextRow, "B").Value = dataB
            .Cells(nextRow, "C").Value = dataC
            .Cells(nextRow, "D").Value = dataD
            .Cells(nextRow, "E").Value = dataE
            .Cells(nextRow, "F").Value = dataF
                    End If
            
    End With
    
    Windows("CRForm.xlsm").Activate
    ActiveWindow.Close
    ExitSub:
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by xladept; 02-16-2014 at 07:09 PM. Reason: alternative code
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    12-19-2012
    Location
    Woodbridge, Virginia
    MS-Off Ver
    Excel 2010
    Posts
    111

    Re: Check for Duplications when transferring data from One Workbook to Master Workbook

    Hi xladept,

    Thank you so much for your quick reply. I tried implementing your code today but kept getting an out of script error. It turns out that it doesn't like
    Q(i, 2)..Q(i, 3)..etc
    so after a few failed attempts, I ended up changing it to
    .Cells(i, 2)
    which got it to work. Thank you again for your help with this. Your great thinking was much appreciated! I will mark this as solved and "Add Reputation."

    J

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Check for Duplications when transferring data from One Workbook to Master Workbook

    Perplexing - that worksheet array code usually works for me

    I'm glad you got it to work and thanks for the rep!

    Oh! I found the problem Change Q(i,2) to Q(1,2) etc.
    Last edited by xladept; 02-18-2014 at 03:27 PM. Reason: Late Fix

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copy paste last row from each workbook in master workbook on header criteria
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2013, 07:28 AM
  2. Transfer/Copy Data from one workbook to another
    By scottw1988 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-26-2012, 09:46 PM
  3. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  4. Replies: 5
    Last Post: 09-20-2010, 04:58 PM
  5. Transfer figures from workbook to master
    By neodjandre in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-15-2007, 08:04 AM

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