+ Reply to Thread
Results 1 to 7 of 7

Prevent Duplicate Row & Population of Last Scan

Hybrid View

Hackboss007 Prevent Duplicate Row &... 05-09-2016, 03:57 PM
AlphaFrog Re: Prevent Duplicate Row &... 05-09-2016, 05:00 PM
Hackboss007 Re: Prevent Duplicate Row &... 05-10-2016, 07:27 AM
AlphaFrog Re: Prevent Duplicate Row &... 05-11-2016, 12:07 AM
Hackboss007 Re: Prevent Duplicate Row &... 05-13-2016, 03:57 PM
AlphaFrog Re: Prevent Duplicate Row &... 05-13-2016, 06:14 PM
Hackboss007 Re: Prevent Duplicate Row &... 05-15-2016, 01:34 PM
  1. #1
    Registered User
    Join Date
    02-13-2014
    Location
    FL, United States
    MS-Off Ver
    Excel 2007
    Posts
    33

    Prevent Duplicate Row & Population of Last Scan

    Hi everyone,

    I'd be obliged if someone can provide some additional help with an existing macro that I was able to find online. The current macro is an inventory counting tool that verifies the barcode scanned in column J2 with the barcode in column D. If a match is found, the count in column F against that row is increased by 1. In addition to this function that the macro provides, I would like to prevent duplicate rows from being entered into the sheet as it causes issues with the count. Column B & Column C would be the only 2 columns being analyzed for duplicates (i.e. if the same part number and batch number is entered twice on the sheet, the macro will display a message and prevent the creation of that data. Identical batch numbers can exist against different part numbers and vice versa). Lastly, I'd like if the details of the row that is most currently modified (quantity increased by macro or the user in column F) to be displayed under the heading "Last Scan" (Bin, Part Number, Batch Number, Barcode and Name). I have attached a sample file with the current existing macro. Your assistance is greatly appreciated.

    Thanks!
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Prevent Duplicate Row & Population of Last Scan

    You could use the Data Validation feature to prevent duplicates.
    • Select columns B and C
    • Select from the menu Data\Data Validation
    • On the DV Dialog:
      • Allow: Custom
      • Formula: =COUNTIFS($B:$B,$B1,$C:$C,$C1)<=1
      • Define the message on the Error Alert tab




    This will put the Last Scan in column L

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Const SCAN_CELL As String = "J2"
        Const RANGE_BC As String = "D1:D10000"
        Dim val, f As Range, rngCodes As Range
        
        If Target.Cells.Count > 1 Then Exit Sub
        If Not Intersect(Target, Me.Range(SCAN_CELL)) Is Nothing Then
        
            val = Trim(Target.Value)
            If Len(val) = 0 Then Exit Sub
        
            Set rngCodes = Me.Range(RANGE_BC)
        
            Set f = rngCodes.Find(val, , xlValues, xlWhole)
            If Not f Is Nothing Then
                With f.Offset(0, 2)
                    .Value = .Value + 1
                End With
            Else
                Set f = rngCodes.Cells(rngCodes.Cells.Count).End(xlUp).Offset(1, 0)
                f.Value = val
                f.Offset(0, 4).Value = "Not Found. Please Reference Maintenix System"
                f.Offset(0, 2).Value = 1
            End If
        
            Application.EnableEvents = False
            Target.Value = ""
            Application.EnableEvents = True
        
        ElseIf Target.Column = 6 Then   'Column F
            Range("L2").Value = Range("A" & Target.Row).Value
            Range("L3").Value = Range("B" & Target.Row).Value
            Range("L4").Value = Range("C" & Target.Row).Value
            Range("L5").Value = Range("D" & Target.Row).Value
            Range("L6").Value = Range("G" & Target.Row).Value
        End If
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    02-13-2014
    Location
    FL, United States
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: Prevent Duplicate Row & Population of Last Scan

    Hi AlphaFrog,

    Thanks so much for the help. The last scan works great! I attempted the data validation solution but it doesn't seem to be working. I tried it prior to posting on the forum as well with no success and so I decided that I may need a macro for it. I set it as custom and used the COUNTIFS formula and set an error alert message but when I enter duplicates, it allows me to proceed. Any assistance you can provide would be greatly appreciated. Thanks again!

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Prevent Duplicate Row & Population of Last Scan

    You're welcome.

    That DV formula should work.
    Before entering the DV formula, did you select both columns B and C (the entire columns not just a subrange) ?
    Did you copy\paste the formula from my post as-is or type it in?
    When you typed in the duplicates, were the two entries an exact match including any trailing spaces?
    Do you type in entries or paste them?

  5. #5
    Registered User
    Join Date
    02-13-2014
    Location
    FL, United States
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: Prevent Duplicate Row & Population of Last Scan

    Hi AlphaFrog,

    Sorry for the delay in getting back to you as a few other things came up along the way. I attempted the DV again and it works but differently from my expectation. The DV prevents duplicates from existing in the same column (either B or C). I was hoping to prevent duplication of a unique combination (i.e. duplicates can exist in B or C but the combination of the two should be unique). For example, the same part number can exist in multiple rows of col. B and the same batch number can exist in multiple rows of col. C but the combination of part number and batch number should be unique. The DV also doesn't seem to pick up the duplicates (with the current logic employed) during a copy and paste. Your help is greatly appreciated and valued. Have a great evening!

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Prevent Duplicate Row & Population of Last Scan

    Try this...

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Const SCAN_CELL As String = "J2"
        Const RANGE_BC As String = "D1:D10000"
        Dim val, f As Range, rngCodes As Range
        Dim rngRow As Range
        
        If Target.Cells.Count = 1 Then
            If Not Intersect(Target, Me.Range(SCAN_CELL)) Is Nothing Then
                
                val = Trim(Target.Value)
                If Len(val) = 0 Then Exit Sub
                
                Set rngCodes = Me.Range(RANGE_BC)
                
                Set f = rngCodes.Find(val, , xlValues, xlWhole)
                If Not f Is Nothing Then
                    With f.Offset(0, 2)
                        .Value = .Value + 1
                    End With
                Else
                    Set f = rngCodes.Cells(rngCodes.Cells.Count).End(xlUp).Offset(1, 0)
                    f.Value = val
                    f.Offset(0, 4).Value = "Not Found. Please Reference Maintenix System"
                    f.Offset(0, 2).Value = 1
                End If
                
                Application.EnableEvents = False
                Target.Value = ""
                Application.EnableEvents = True
                
            ElseIf Target.Column = 6 Then   'Column F
                Range("L2").Value = Range("A" & Target.Row).Value
                Range("L3").Value = Range("B" & Target.Row).Value
                Range("L4").Value = Range("C" & Target.Row).Value
                Range("L5").Value = Range("D" & Target.Row).Value
                Range("L6").Value = Range("G" & Target.Row).Value
            End If
        End If
        
        
        'Data Validation for columns B:C
        If Not Intersect(Columns("B:C"), Target) Is Nothing Then
            For Each rngRow In Target.EntireRow.Columns("B:C").Rows
                If rngRow.Cells(1, 1) <> "" And rngRow.Cells(1, 2) <> "" Then
                    If Evaluate("=SUMPRODUCT(--(" & Me.UsedRange.Columns("B").Address & "&" & Me.UsedRange.Columns("C").Address & "=" & rngRow.Cells(1, 1).Address & "&" & rngRow.Cells(1, 2).Address & "))>1") Then
                        MsgBox "PN: " & rngRow.Cells(1, 1) & vbLf & _
                               "SN: " & rngRow.Cells(1, 2), vbExclamation, "Duplicate Entry"
                        Application.EnableEvents = False
                        Intersect(rngRow, Target).ClearContents
                        Application.EnableEvents = True
                    End If
                End If
            Next
        End If
        
    End Sub

  7. #7
    Registered User
    Join Date
    02-13-2014
    Location
    FL, United States
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: Prevent Duplicate Row & Population of Last Scan

    Hi AlphaFrog,

    The modified code works great! Thanks a ton!

+ 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. [SOLVED] Prevent duplicate entries
    By Blokeman in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-06-2015, 07:24 PM
  2. RFID Time and date stamp on scan and off scan
    By forey89 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2015, 06:46 AM
  3. Scan-in, Scan-out tool inventory with barcode scanner
    By rycr023 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-20-2014, 11:52 AM
  4. Replies: 0
    Last Post: 06-14-2012, 12:38 PM
  5. How to prevent duplicate value printing ?
    By daksh1981 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-13-2010, 11:30 AM
  6. scan recipient address, display warning (prevent reply to listserv)
    By as_sass in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 04-26-2010, 06:49 AM
  7. Prevent virus scan of Excel workbook
    By josh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-30-2005, 04:05 PM

Tags for this Thread

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