+ Reply to Thread
Results 1 to 2 of 2

Not allow duplicates from listbox to Worksheet

  1. #1
    Forum Contributor
    Join Date
    01-06-2004
    Location
    Carbondale CO
    Posts
    245

    Not allow duplicates from listbox to Worksheet

    Hi,
    I have a UserForm that allows multi-selection from a listbox and a CommandButton to send the selected items to the worksheet.
    The UserForm is Modeless and has several CommandButtons to change the rowsource of the listbox. It is now possible to select and enter the same item twice into the sheet, which would create an error in the sheet not easily found.
    So I need to be able to check that a listbox selection is not a duplicate already on the sheet and prevent it from being sent to the sheet. Ideally, any remaining non-duplicate items would be sent to the sheet only the duplicate stopped.
    My current code has a MsgBox, but I put that in the code as a way to alert me during design. I would love to have this all in the background, without any notification.
    My current Code generates a 424 run-time error Object Required. I have run out of ideas how to fix this.

    Other info:
    ItemDescripTO is a horizontal single row subset named range of the named range TakeOffHeaders (Rng1) the data from the listbox goes in to.

    Here is the Code:

    Option Explicit

    Private Sub cmdEnterSelection_Click()
    Dim ACol As Long
    Dim Rng1 As Range
    Dim rng As Range
    Dim i As Long
    Dim j As Long
    Dim Entries As Long
    Dim CopyCol As Long
    Application.ScreenUpdating = False
    ACol = Sheets("Takeoff").Range("AlphaCol").Column
    Set Rng1 = Sheets("Takeoff").Range("TakeOffHeaders")
    Set rng = Sheets("Takeoff").Range("ScopeNames")
    Entries = Excel.WorksheetFunction.CountA(rng)
    CopyCol = 1 + Entries
    For i = 0 To Me.ListBox1.ListCount - 1
    If Me.ListBox1.Selected(i) Then
    Columns(ACol).Copy
    Columns(ACol + CopyCol - 1).Select
    ActiveSheet.Paste
    If WorksheetFunction.CountIf(Range("ItemDescripTO"), _
    ListBox1.List(i, 0).Value) = 0 Then '<<< 424 Error Hits Here
    With Rng1
    .Cells(1, CopyCol).Value = ListBox1.List(i, 0)
    .Cells(2, CopyCol).Value = ListBox1.List(i, 1)
    .Cells(4, CopyCol).Value = ListBox1.List(i, 2)
    .Cells(5, CopyCol).Value = ListBox1.List(i, 3)
    .Cells(7, CopyCol).Value = ListBox1.List(i, 4)
    .Cells(8, CopyCol).Value = ListBox1.List(i, 5)
    .Cells(9, CopyCol).Value = ListBox1.List(i, 6)
    .Cells(10, CopyCol).Value = ListBox1.List(i, 7)
    End With
    Else
    MsgBox "One of your selections is a" _
    & " duplicate."
    End If
    CopyCol = CopyCol + 1

    End If
    Next i
    Entries = Excel.WorksheetFunction.CountA(rng)
    CopyCol = 1 + Entries
    Columns(ACol + CopyCol).Clear
    OptionButton2.Value = True
    ActiveSheet.Range("E12").Select
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    Last edited by Casey; 06-01-2006 at 10:55 AM.
    Casey

  2. #2
    Forum Contributor
    Join Date
    01-06-2004
    Location
    Carbondale CO
    Posts
    245

    Avoiding Duplicates

    Great VBA Gurus,
    Please take a look at my post from yesterday. I have searched in the archives until my eyes are crossed and can't find anything new to try. The code I posted yesterday is still the best I can come up with.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1