PC Review


Reply
Thread Tools Rate Thread

delete single occurances in a string

 
 
J.W. Aldridge
Guest
Posts: n/a
 
      21st May 2010

i have headers in a5:g5
I have data in columns A6:G5000.

based on the series of numbers in column b, i want to delete all
single occurances of the number found.

In other words, if it is not listed/found in column B at least twice,
delete the entire row.
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      21st May 2010
I would add a new column C (just temporarily).

Then in C6, I'd add this formula:
=if(countif($b$6:$b$5000,b6)>1,"ok",na())
and drag down.

This would result in an error on every row that had exactly one occurrence.

Then I'd select that range
convert to values (to remove the slow calculation)
Edit|goto|special|constants and errors
delete those selected rows
delete column C.

In code:

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim myRng As Range

Set wks = Worksheets("Sheet1")

With wks

Set myRng = .Range("B5:B5000")

myRng.Cells(1).Offset(0, 1).EntireColumn.Insert

With myRng.Offset(0, 1)
.Formula = "=if(countif(" & myRng.Address & "," _
& myRng.Cells(1).Address(0, 0) & ")>1,""ok"",na())"
.Value = .Value
End With

On Error Resume Next 'in case there are no errors
myRng.Offset(0, 1).Cells.SpecialCells(xlCellTypeConstants, xlErrors) _
.EntireRow.Delete
On Error GoTo 0

'remove the helper column
myRng.Cells(1).Offset(0, 1).EntireColumn.Delete

End With

End Sub

"J.W. Aldridge" wrote:
>
> i have headers in a5:g5
> I have data in columns A6:G5000.
>
> based on the series of numbers in column b, i want to delete all
> single occurances of the number found.
>
> In other words, if it is not listed/found in column B at least twice,
> delete the entire row.


--

Dave Peterson
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      21st May 2010
I think this macro should do what you want (just change the example
worksheet name I used in the With statement to your actual worksheet's
name)...

Sub DeleteSingleEntriesInColumnB()
Dim X As Long, LastRow As Long, U As Range
Const FirstRow As Long = 6
With Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For X = FirstRow To LastRow
If WorksheetFunction.CountIf(.Range("B" & FirstRow & _
":B" & LastRow), .Cells(X, "B")) = 1 Then
If U Is Nothing Then
Set U = .Rows(X)
Else
Set U = Union(U, .Rows(X))
End If
End If
Next
End With
U.Delete
End Sub

--
Rick (MVP - Excel)



"J.W. Aldridge" <(E-Mail Removed)> wrote in message
news:5950740d-8088-4888-be99-(E-Mail Removed)...
>
> i have headers in a5:g5
> I have data in columns A6:G5000.
>
> based on the series of numbers in column b, i want to delete all
> single occurances of the number found.
>
> In other words, if it is not listed/found in column B at least twice,
> delete the entire row.


 
Reply With Quote
 
J.W. Aldridge
Guest
Posts: n/a
 
      21st May 2010
Thanx...

Works perfectly.

Only thing is...

If I wanted to change the worksheets to the current/ active sheet...

Set wks = Worksheets("Sheet1")
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      21st May 2010
Just replace Worksheets("Sheet1") with ActiveSheet and that should do it.

You can do this with the code I post also in case you want to try it out (my
code does not insert/delete any columns... it just works with the data you
have).

--
Rick (MVP - Excel)



"J.W. Aldridge" <(E-Mail Removed)> wrote in message
news:eead44fc-3e43-401b-9034-(E-Mail Removed)...
> Thanx...
>
> Works perfectly.
>
> Only thing is...
>
> If I wanted to change the worksheets to the current/ active sheet...
>
> Set wks = Worksheets("Sheet1")


 
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
keep duplicated numbers, eliminate single occurances J.W. Aldridge Microsoft Excel Programming 1 16th Jul 2010 03:31 PM
Count Occurances of a txt string flumpuk Microsoft Excel Misc 1 1st Sep 2007 01:04 PM
Count Occurances of String in a Column? Bill Microsoft Excel Misc 4 23rd Feb 2006 12:30 AM
Count of Occurances of a Character in a String - BCP =?Utf-8?B?QkNQ?= Microsoft Access External Data 1 28th Oct 2005 11:00 PM
Multiple Occurances Of Value In String =?Utf-8?B?SmltIEhlYXZleQ==?= Microsoft ASP .NET 4 29th Jun 2004 03:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:16 PM.