有没有办法比较excel中的更改列并删除重复项?

问题描述 投票:0回答:1

所以我试图在excel中实现条形码扫描器。它已经作为单元格中的数字输入条形码。在另一栏中,我有一组条形码。因此,当扫描仪提供条形码时,应删除此条形码和另一列中的一个副本。

我已经尝试过excel compare-tables函数,但它既没有删除重复的条形码,也没有能够在条形码列中标记重复项。这意味着:如果条形码列中有两次代码123456,则会将其标记为重复。

所以想象我有条形码123,123,124,125。然后,如果我扫描124,我希望该列仅包含123,123,125,如果我扫描123,我希望它包含123,125。

有没有办法在excel中做到这一点,或者我是否需要特定的软件,如果有,那么使用哪个?

My problem image

excel barcode
1个回答
0
投票

您可以尝试在工作表更改事件中导入以下代码,修改它并尝试:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim BarCode As String
    Dim LastRowA As Long, LastRowC As Long, i As Long, Times As Long
    Dim arr As Variant

    'Let us assume that:
    '1. All bar codes appears in column A
    '2. The Selected bar code are in cell B2
    '3. And the results are in column C

    With ThisWorkbook.Worksheets("Sheet1")

        If Not Intersect(Target, .Range("B2")) Is Nothing And Target.Count = 1 Then '<- Check if there is any change in cell B2

            LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row

            Application.EnableEvents = False

                If LastRowC > 1 Then

                    .Range("C2:C" & LastRowC).Clear '<- If there are data in the results field clear them

                End If

                BarCode = Target.Value '<- Get the code imported

                LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

                arr = .Range("A2:A" & LastRowA) '<- Create an array with all the bar codes

                For i = LBound(arr) To UBound(arr) '<- Loop codes
                    Debug.Print arr(i, 1)
                    If arr(i, 1) = BarCode Then
                        Times = Times + 1
                        If Times > 1 Then
                            LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row
                            .Range("C" & LastRowC + 1).Value = arr(i, 1)
                        End If
                    Else
                        LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row
                        .Range("C" & LastRowC + 1).Value = arr(i, 1)
                    End If

            Next i

            Application.EnableEvents = True

        End If

    End With

End Sub

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.