Pasting values from listbox to excel spreadsheet but skipping values.

VBAhelp33

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

Teaching myself VBA and I have created the below code that pastes 2 columns of data from a listbox2 to an excel worksheet. However this code does not recognize duplicates already in the excel worksheet. Is there a way I can skip an entry if it already is in the excel spreadsheet?

Also I sometimes get an "out of memory" warning when I use the below code. If anyone can see any obvious issues can you please let me know?

Many thanks

VBA Code:
Private Sub CommandButton1_Click()

Application.ScreenUpdating = False

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

Dim PS As Worksheet
Set PS = Worksheets("Data")

For i = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox2.Selected(i) = True And Me.ListBox2.List(i, 1) <> "" Then
    PS.Range("E52").End(xlUp).Offset(1, 0) = Me.ListBox2.List(i, 0)
    PS.Range("E52").End(xlUp).Offset(0, 1) = Me.ListBox2.List(i, 1)

Me.ListBox2.Selected(i) = False
End If
Next i

Me.ListBox2.Clear
Application.ScreenUpdating = True

Unload Me

End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,213,558
Messages
6,114,296
Members
448,564
Latest member
ED38

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