有没有办法比较 excel 中的更改列并删除重复项?
Is there a way to compare to changing columns in excel and delete duplicates?
所以我正在尝试在 excel 中实现条码扫描器。它已经将条形码作为数字输入到单元格中。在另一列中,我有一组条形码。所以当扫码器提供一个条码时,这个条码和其他栏中重复的条码被删除。
我已经尝试过excel比较表功能,但是它既不能删除重复的条码,也不能在条码列中不标记重复项。这意味着:如果代码 123456 在条形码列中出现两次,它将被标记为重复。
假设我有条形码 123,123,124,125。然后,如果我扫描 124,我希望该列仅包含 123,123,125,如果我扫描 123,我希望它包含 123,125。
在 excel 中有没有办法做到这一点,或者我需要特定的软件,如果需要,那么使用哪个?
My problem image
您可以尝试在 sheet 更改事件中导入以下代码,修改并尝试:
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
结果:
所以我正在尝试在 excel 中实现条码扫描器。它已经将条形码作为数字输入到单元格中。在另一列中,我有一组条形码。所以当扫码器提供一个条码时,这个条码和其他栏中重复的条码被删除。
我已经尝试过excel比较表功能,但是它既不能删除重复的条码,也不能在条码列中不标记重复项。这意味着:如果代码 123456 在条形码列中出现两次,它将被标记为重复。
假设我有条形码 123,123,124,125。然后,如果我扫描 124,我希望该列仅包含 123,123,125,如果我扫描 123,我希望它包含 123,125。
在 excel 中有没有办法做到这一点,或者我需要特定的软件,如果需要,那么使用哪个?
My problem image
您可以尝试在 sheet 更改事件中导入以下代码,修改并尝试:
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
结果: