+ Reply to Thread
Results 1 to 2 of 2

Transfer Listbox column into Excel Range

  1. #1
    Registered User
    Join Date
    09-09-2009
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    1

    Transfer Listbox column into Excel Range

    Hello,

    I'm new here

    I suck *** with VB but I'm trying to do something that shouldn't be too hard. I have a column of events that occur. The column consists of several unique events repeated over and over. I am trying to automate a way to filter that list to just the unique events. I came across this :

    Option Explicit
    ' This example is based on a tip by J.G. Hussey,
    ' published in "Visual Basic Programmer's Journal"

    Sub RemoveDuplicates()
    Dim AllCells As Range, Cell As Range
    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item
    Dim lItem As Long

    ' The items are in A1:A105
    Set AllCells = Range("A2:A262")

    ' The next statement ignores the error caused
    ' by attempting to add a duplicate key to the collection.
    ' The duplicate is not added - which is just what we want!
    On Error Resume Next
    For Each Cell In AllCells
    NoDupes.Add Cell.Value, CStr(Cell.Value)
    ' Note: the 2nd argument (key) for the Add method must be a string
    Next Cell

    ' Resume normal error handling
    On Error GoTo 0

    ' Update the labels on UserForm1
    With UserForm1
    .Label1.Caption = "Total Items: " & AllCells.Count
    .Label2.Caption = "Unique Items: " & NoDupes.Count
    End With

    ' Sort the collection (optional)
    For i = 1 To NoDupes.Count - 1
    For j = i + 1 To NoDupes.Count
    If NoDupes(i) > NoDupes(j) Then
    Swap1 = NoDupes(i)
    Swap2 = NoDupes(j)
    NoDupes.Add Swap1, before:=j
    NoDupes.Add Swap2, before:=i
    NoDupes.Remove i + 1
    NoDupes.Remove j + 1
    End If
    Next j
    Next i

    ' Add the sorted, non-duplicated items to a ListBox
    For Each Item In NoDupes
    UserForm1.ListBox1.AddItem Item
    Next Item

    ' Show the UserForm
    UserForm1.Show
    End Sub

    This works and the listbox displays the correct thing. However I dont know how to get the listbox data back into an excel range. Can someone help me plz?

    Thanks!!!
    Philip

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Transfer Listbox column into Excel Range

    Welcome to the forum,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

+ 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