Hello, I wrote this code and it checks in the text box that there is no duplicate data to be added and it works correctly (the input values in the text box are multiple and entered below each other)
I want to have a code that checks if the value already exists in column A and shows it and does not just add the same value and adds the rest.
tank u
I want to have a code that checks if the value already exists in column A and shows it and does not just add the same value and adds the rest.
tank u
VBA Code:
Private Sub CommandButton3_Click()
Dim Target As Range
Dim Data As Variant
Dim DuplicateNumbers As String
Dim i As Long, j As Long
Dim DuplicateFound As Boolean
If TextBox1.Text = "" Then Exit Sub
Data = Split(TextBox1.Text, vbCrLf)
With Worksheets("sheet1")
Set Target = .Range("A" & .Rows.Count).End(xlUp)
If Target.Value <> "" Then Set Target = Target.Offset(1)
' Check for duplicate numbers
For i = LBound(Data) To UBound(Data)
DuplicateFound = False
For j = i + 1 To UBound(Data)
If Data(i) = Data(j) Then
DuplicateNumbers = DuplicateNumbers & Data(i) & vbCrLf
DuplicateFound = True
Exit For
End If
Next j
If DuplicateFound = False Then
Target.Value = Data(i)
Set Target = Target.Offset(1)
End If
Next i
End With
' Display duplicate numbers
If DuplicateNumbers <> "" Then
MsgBox "Duplicate Numbers:" & vbCrLf & DuplicateNumbers
End If
End Sub
Book1 (1).xlsm | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | number | |||||
2 | 2 | |||||
3 | 3 | |||||
4 | 4 | |||||
5 | 5 | |||||
6 | 43 | |||||
7 | 44 | |||||
8 | 2 | |||||
9 | 2 | |||||
10 | 2 | |||||
11 | 3 | |||||
12 | 3 | |||||
13 | ||||||
Sheet1 |