Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 363
Default Duplicate value are not being removed

The following code is suppose to remove the duplicate values, but it is not.

Listbox1.value when selected populates the listbox2

Is use the same code to initialise the form and populate Listbox1 which DOES remove any duplicates.
But, the below code does not.
Am i missing something obvious ?

Private Sub ListBox1_Change()
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection
On Error Resume Next
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
.Select
Set NoDupes = New Collection
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < "" Then
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still DISPLAYED
End If
Else
Err.Clear
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Duplicate value are not being removed

I'm see ListBox1_Change and you say you are trying to apply this to ListBox2?
Or am I just confused?

"Corey" wrote:

The following code is suppose to remove the duplicate values, but it is not.

Listbox1.value when selected populates the listbox2

Is use the same code to initialise the form and populate Listbox1 which DOES remove any duplicates.
But, the below code does not.
Am i missing something obvious ?

Private Sub ListBox1_Change()
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection
On Error Resume Next
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
.Select
Set NoDupes = New Collection
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < "" Then
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still DISPLAYED
End If
Else
Err.Clear
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 363
Default Duplicate value are not being removed

When a slection is made to Listbox1, Listbox2 is then Poplulated with values.


Corey...

"JLGWhiz" wrote in message
...
I'm see ListBox1_Change and you say you are trying to apply this to ListBox2?
Or am I just confused?

"Corey" wrote:

The following code is suppose to remove the duplicate values, but it is not.

Listbox1.value when selected populates the listbox2

Is use the same code to initialise the form and populate Listbox1 which DOES remove any
duplicates.
But, the below code does not.
Am i missing something obvious ?

Private Sub ListBox1_Change()
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection
On Error Resume Next
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
.Select
Set NoDupes = New Collection
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < "" Then
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still DISPLAYED
End If
Else
Err.Clear
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Duplicate value are not being removed

Maybe I'm just reading the code wrong, but I don't see anything in the
snippet you posted that would prevent duplicates. The If statements could
evaluate to true as far as I can tell, which means the item gets added.

"Corey" wrote:

When a slection is made to Listbox1, Listbox2 is then Poplulated with values.


Corey...

"JLGWhiz" wrote in message
...
I'm see ListBox1_Change and you say you are trying to apply this to ListBox2?
Or am I just confused?

"Corey" wrote:

The following code is suppose to remove the duplicate values, but it is not.

Listbox1.value when selected populates the listbox2

Is use the same code to initialise the form and populate Listbox1 which DOES remove any
duplicates.
But, the below code does not.
Am i missing something obvious ?

Private Sub ListBox1_Change()
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection
On Error Resume Next
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
.Select
Set NoDupes = New Collection
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < "" Then
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still DISPLAYED
End If
Else
Err.Clear
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Duplicate value are not being removed

Corey,

I didn't actually run this so not sure if it will work. But a couple of
things:

You only want to set the collection once. As you add to it, if it's a dupe
that will generate an error, which will tell you not add it. So you want to
bracket the adding with your 2 On Error statements.

I've used this technique a few times. I always fill the whole collection
first and then write it to the range. Anyways this might at least get you
closer:

Private Sub ListBox1_Change()
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection

Set NoDupes = New Collection
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < ""
Then
On Error Resume Next
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still
DISPLAYED
On Error Goto 0
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub


"Corey" wrote in message
...
The following code is suppose to remove the duplicate values, but it is
not.

Listbox1.value when selected populates the listbox2

Is use the same code to initialise the form and populate Listbox1 which
DOES remove any duplicates.
But, the below code does not.
Am i missing something obvious ?

Private Sub ListBox1_Change()
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection
On Error Resume Next
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
.Select
Set NoDupes = New Collection
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < ""
Then
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value
Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still
DISPLAYED
End If
Else
Err.Clear
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 363
Default Duplicate value are not being removed

Thnak You Doug your advice is appreciated.

Working greate now, thank You


Corey....


"Doug Glancy" wrote in message
...
Corey,

I didn't actually run this so not sure if it will work. But a couple of
things:

You only want to set the collection once. As you add to it, if it's a dupe
that will generate an error, which will tell you not add it. So you want to
bracket the adding with your 2 On Error statements.

I've used this technique a few times. I always fill the whole collection
first and then write it to the range. Anyways this might at least get you
closer:

Private Sub ListBox1_Change()
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection

Set NoDupes = New Collection
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < ""
Then
On Error Resume Next
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still
DISPLAYED
On Error Goto 0
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub


"Corey" wrote in message
...
The following code is suppose to remove the duplicate values, but it is
not.

Listbox1.value when selected populates the listbox2

Is use the same code to initialise the form and populate Listbox1 which
DOES remove any duplicates.
But, the below code does not.
Am i missing something obvious ?

Private Sub ListBox1_Change()
Application.ScreenUpdating = False
If ListBox2.ListCount 0 Then ListBox2.Clear
Dim LastCell As Long
Dim myrow As Long
Dim NoDupes As Collection
On Error Resume Next
LastCell = Worksheets("Data2").Cells(Rows.Count, "C").End(xlUp).Row
With ActiveWorkbook.Worksheets("Data2")
.Select
Set NoDupes = New Collection
For myrow = 1 To LastCell
If ListBox1.Value = .Cells(myrow, 2).Value And .Cells(myrow, 2) < ""
Then
NoDupes.Add .Cells(myrow, 3).Value, CStr(.Cells(myrow, 3).Value)
If Err.Number = 0 Then
If .Cells(myrow, 3) < "" And .Cells(myrow, 2) = ListBox1.Value
Then
ListBox2.AddItem .Cells(myrow, 3) ' <=== DUPLICATES are still
DISPLAYED
End If
Else
Err.Clear
End If
End If
Next
End With
Sheets("POST Project Report").Select
Application.ScreenUpdating = True
End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
RIBBON TAB NOT BEING REMOVED FARAZ QURESHI Excel Discussion (Misc queries) 5 January 2nd 09 06:15 PM
Why is qw in a cell and cannot be removed? erinping Excel Discussion (Misc queries) 5 August 30th 07 11:56 AM
Duplicate value are removed, but how can i display in tx8 the amount of duplicates there are ? [email protected] Excel Programming 0 March 1st 07 10:14 AM
How Is CHOOSESHEET.XLA Removed? kghexce Excel Discussion (Misc queries) 2 January 10th 06 09:46 AM
removed vba code Michaela[_2_] Excel Programming 0 January 22nd 04 10:10 AM


All times are GMT +1. The time now is 09:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"