Pasting from listbox to spreadsheet - is there a way to avoid duplicate entries?

VBAhelp33

New Member
Joined
Sep 13, 2020
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hi all

I am pasting data from 2 columns in a listbox to an excel spreadsheet. Is there a way I can avoid duplicate entries into the worksheet if they are already entered? I would also like to be able to tell the user which entry is a duplicate.

Many thanks

VBA Code:
Private Sub CommandButton4_Click() 'paste selected items in listbox2 to excel worksheet

Application.ScreenUpdating = False

Dim I As Long
For I = 0 To Me.ListBox2.ListCount - 1
  ListBox2.Selected(I) = True
    
Next

Dim rngNext As Range
Dim col As Long

    Set rngNext = Worksheets("data").Range("E" & Rows.Count).End(xlUp).Offset(1)
    For I = 0 To ListBox2.ListCount - 1
    
        If ListBox2.Selected(I) Then
            For col = 0 To ListBox2.ColumnCount - 1
                rngNext.Offset(, col).Value = ListBox2.List(I, col)
            Next col
            
            Set rngNext = rngNext.Offset(1)
            ListBox2.Selected(I) = False
            
        End If
            
    Next I
       

Application.ScreenUpdating = True

BeforeExit:

Set rngNext = Nothing
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Something like this
VBA Code:
For col = 0 To ListBox2.ColumnCount - 1
    If Application.CountIf(Columns(col), ListBox2.List(I, col)) = 0 Then
                rngNext.Offset(, col).Value = ListBox2.List(I, col)
    Else
        MsgBox "Duplicate detected in column  " & col
        'Add code here to Exit sub, go to handler, etc.
    End If
Next col
 
Upvote 0
Thanks for your reply
What should Columns be?

VBA Code:
If Application.CountIf(COLUMNS(col), ListBox2.List(I, col)) = 0 Then
 
Upvote 0
You stated you wanted to check for duplicate entries, but did not specify where. The code I suggested would check each column that you are addressing to see if it contains the same value you are about to enter into the cell.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top