PC Review


Reply
Thread Tools Rate Thread

Compare Contents of 2 listboxes

 
 
LaDdIe
Guest
Posts: n/a
 
      7th Jan 2009
Could I please have assistance with the code below, my aim is to compare
contents of ListBox2 with ListBox1 if a match is found to remove it from
ListBox1;

Dim i As Long, j As Long

For i = 0 To ListBox2.ListCount - 1
DoEvents
ListBox2.ListIndex = i
For j = 0 To ListBox1.ListCount - 1
DoEvents
ListBox1.ListIndex = j
If InStr(1, ListBox2.List(ListBox2.ListIndex),
ListBox1.List(ListBox1.ListIndex)) Then
ListBox1.RemoveItem (j)
End If
Next j
Next i

End Sub

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      7th Jan 2009
This worked ok for me:

Option Explicit
Private Sub CommandButton1_Click()

Dim iCtr As Long
Dim res As Variant
Dim myArr() As String

With Me.ListBox2
ReDim myArr(0 To .ListCount - 1)
For iCtr = 0 To .ListCount - 1
myArr(iCtr) = .List(iCtr, 0)
Next iCtr
End With

With Me.ListBox1
'bottom up
For iCtr = .ListCount - 1 To 0 Step -1
res = Application.Match(.List(iCtr), myArr, 0)
If IsError(res) Then
'no match, keep it
Else
.RemoveItem iCtr
End If
Next iCtr
End With

End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long

With Me.ListBox1
.ColumnCount = 1
For iCtr = 1 To 5
.AddItem "a" & iCtr
Next iCtr
For iCtr = 9 To 15
.AddItem "a" & iCtr
Next iCtr
End With

With Me.ListBox2
.ColumnCount = 1
For iCtr = 3 To 7
.AddItem "a" & iCtr
Next iCtr
End With
End Sub




LaDdIe wrote:
>
> Could I please have assistance with the code below, my aim is to compare
> contents of ListBox2 with ListBox1 if a match is found to remove it from
> ListBox1;
>
> Dim i As Long, j As Long
>
> For i = 0 To ListBox2.ListCount - 1
> DoEvents
> ListBox2.ListIndex = i
> For j = 0 To ListBox1.ListCount - 1
> DoEvents
> ListBox1.ListIndex = j
> If InStr(1, ListBox2.List(ListBox2.ListIndex),
> ListBox1.List(ListBox1.ListIndex)) Then
> ListBox1.RemoveItem (j)
> End If
> Next j
> Next i
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      7th Jan 2009
'Sub can be called from a UserForm event.
'Assumes the Listboxes are not bound to a worksheet range.
'Assumes data is loaded into Listboxes before this sub is called...
'--
Sub MakeDifferent()
Dim i As Long
Dim j As Long

For i = 0 To UserForm1.ListBox2.ListCount - 1
For j = UserForm1.ListBox1.ListCount - 1 To 0 Step -1
If UserForm1.ListBox2.List(i, 0) = _
UserForm1.ListBox1.List(j, 0) Then
UserForm1.ListBox1.RemoveItem (j)
End If
Next 'j
Next 'i
End Sub
--
Jim Cone
Portland, Oregon USA



"LaDdIe"
<(E-Mail Removed)>
wrote in message
Could I please have assistance with the code below, my aim is to compare
contents of ListBox2 with ListBox1 if a match is found to remove it from
ListBox1;

Dim i As Long, j As Long

For i = 0 To ListBox2.ListCount - 1
DoEvents
ListBox2.ListIndex = i
For j = 0 To ListBox1.ListCount - 1
DoEvents
ListBox1.ListIndex = j
If InStr(1, ListBox2.List(ListBox2.ListIndex),
ListBox1.List(ListBox1.ListIndex)) Then
ListBox1.RemoveItem (j)
End If
Next j
Next i

End Sub

 
Reply With Quote
 
LaDdIe
Guest
Posts: n/a
 
      7th Jan 2009
Supa Dupa..... Thanks

"Dave Peterson" wrote:

> This worked ok for me:
>
> Option Explicit
> Private Sub CommandButton1_Click()
>
> Dim iCtr As Long
> Dim res As Variant
> Dim myArr() As String
>
> With Me.ListBox2
> ReDim myArr(0 To .ListCount - 1)
> For iCtr = 0 To .ListCount - 1
> myArr(iCtr) = .List(iCtr, 0)
> Next iCtr
> End With
>
> With Me.ListBox1
> 'bottom up
> For iCtr = .ListCount - 1 To 0 Step -1
> res = Application.Match(.List(iCtr), myArr, 0)
> If IsError(res) Then
> 'no match, keep it
> Else
> .RemoveItem iCtr
> End If
> Next iCtr
> End With
>
> End Sub
> Private Sub UserForm_Initialize()
> Dim iCtr As Long
>
> With Me.ListBox1
> .ColumnCount = 1
> For iCtr = 1 To 5
> .AddItem "a" & iCtr
> Next iCtr
> For iCtr = 9 To 15
> .AddItem "a" & iCtr
> Next iCtr
> End With
>
> With Me.ListBox2
> .ColumnCount = 1
> For iCtr = 3 To 7
> .AddItem "a" & iCtr
> Next iCtr
> End With
> End Sub
>
>
>
>
> LaDdIe wrote:
> >
> > Could I please have assistance with the code below, my aim is to compare
> > contents of ListBox2 with ListBox1 if a match is found to remove it from
> > ListBox1;
> >
> > Dim i As Long, j As Long
> >
> > For i = 0 To ListBox2.ListCount - 1
> > DoEvents
> > ListBox2.ListIndex = i
> > For j = 0 To ListBox1.ListCount - 1
> > DoEvents
> > ListBox1.ListIndex = j
> > If InStr(1, ListBox2.List(ListBox2.ListIndex),
> > ListBox1.List(ListBox1.ListIndex)) Then
> > ListBox1.RemoveItem (j)
> > End If
> > Next j
> > Next i
> >
> > End Sub

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
LaDdIe
Guest
Posts: n/a
 
      7th Jan 2009
Also Supa Dupa

"Jim Cone" wrote:

> 'Sub can be called from a UserForm event.
> 'Assumes the Listboxes are not bound to a worksheet range.
> 'Assumes data is loaded into Listboxes before this sub is called...
> '--
> Sub MakeDifferent()
> Dim i As Long
> Dim j As Long
>
> For i = 0 To UserForm1.ListBox2.ListCount - 1
> For j = UserForm1.ListBox1.ListCount - 1 To 0 Step -1
> If UserForm1.ListBox2.List(i, 0) = _
> UserForm1.ListBox1.List(j, 0) Then
> UserForm1.ListBox1.RemoveItem (j)
> End If
> Next 'j
> Next 'i
> End Sub
> --
> Jim Cone
> Portland, Oregon USA
>
>
>
> "LaDdIe"
> <(E-Mail Removed)>
> wrote in message
> Could I please have assistance with the code below, my aim is to compare
> contents of ListBox2 with ListBox1 if a match is found to remove it from
> ListBox1;
>
> Dim i As Long, j As Long
>
> For i = 0 To ListBox2.ListCount - 1
> DoEvents
> ListBox2.ListIndex = i
> For j = 0 To ListBox1.ListCount - 1
> DoEvents
> ListBox1.ListIndex = j
> If InStr(1, ListBox2.List(ListBox2.ListIndex),
> ListBox1.List(ListBox1.ListIndex)) Then
> ListBox1.RemoveItem (j)
> End If
> Next j
> Next i
>
> End Sub
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare contents of two folders Steve Microsoft Excel Programming 8 29th Apr 2009 12:42 AM
Compare row contents Dylan Microsoft Excel Programming 7 14th Jul 2008 09:05 PM
Compare cell contents Peter Microsoft Excel Discussion 2 29th Sep 2006 11:48 AM
How to compare the contents of two rows is the same? OKLover Microsoft Excel Programming 5 3rd Jul 2005 04:23 PM
Compare contents of 2 fields. Dave Phillabaum Microsoft Access Queries 1 20th May 2004 09:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:34 PM.