Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default Remove data that doesn't match ??##?##

I have data in column A that is in the format of ??##?## i.e. it might be
CD01A01 for instance.

Any column containing this data type I want to keep (including the data in
the row)

I need a code that will keep this sort of data and delete all other rows that
don't match this format

Hope this makes sense


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200510/1
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Remove data that doesn't match ??##?##

Hi Crowbar,

Crowbar via OfficeKB.com wrote:
I have data in column A that is in the format of ??##?## i.e. it
might be CD01A01 for instance.

Any column containing this data type I want to keep (including the
data in the row)

I need a code that will keep this sort of data and delete all other
rows that don't match this format


Here's one way:

Sub test()
Dim rngTarget As Range
Dim lRow As Long
Dim lLastRow As Long

With Worksheets("Sheet1")
lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For lRow = 1 To lLastRow
If Not .Cells(lRow, 1).Value Like "??##?##" Then
If Not rngTarget Is Nothing Then
Set rngTarget = Application.Union(rngTarget, _
.Cells(lRow, 1).EntireRow)
Else
Set rngTarget = .Cells(lRow, 1).EntireRow
End If
End If
Next lRow
End With

If Not rngTarget Is Nothing Then
rngTarget.Delete shift:=xlUp
Set rngTarget = Nothing
End If
End Sub

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Remove data that doesn't match ??##?##

On Wed, 26 Oct 2005 20:59:00 GMT, "Crowbar via OfficeKB.com" <u15117@uwe
wrote:

I have data in column A that is in the format of ??##?## i.e. it might be
CD01A01 for instance.

Any column containing this data type I want to keep (including the data in
the row)

I need a code that will keep this sort of data and delete all other rows that
don't match this format

Hope this makes sense


In Tools/References, select "Microsoft VBScript Regular Expressions 5.5"

Then I believe this code will do what you require. Note that you may need to
change the FirstRow constant as I don't know where you want to start checking.
The LastRow constant is set based on the last entry in Column A.


=============================
Sub RemRw()
Dim regex As Object
Dim i As Long
Dim Temp As String
Dim LastRow As Long

Const FirstRow As Long = 1 'May need to be changed
Const Ptrn As String = "^\w\w\d\d\w\d\d$"

Set regex = CreateObject("vbscript.regexp")
regex.Pattern = Ptrn

LastRow = [A65536].End(xlUp).Row

For i = LastRow To FirstRow Step -1
Temp = Cells(i, 1).Text

If regex.Test(Temp) = False Then
Cells(i, 1).EntireRow.Delete
End If
Next i

End Sub
==========================


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Remove data that doesn't match ??##?##

On Wed, 26 Oct 2005 20:59:00 GMT, "Crowbar via OfficeKB.com" <u15117@uwe
wrote:

I have data in column A that is in the format of ??##?## i.e. it might be
CD01A01 for instance.

Any column containing this data type I want to keep (including the data in
the row)

I need a code that will keep this sort of data and delete all other rows that
don't match this format

Hope this makes sense



Jake's post reminded me of the "Like" operator, which allows me to simplify my
routine considerably. (I've had regular expressions on the brain lately ;-)))

===================
Sub RemRw()
Dim i As Long
Dim Temp As String
Dim LastRow As Long

Const FirstRow As Long = 1 'May need to be changed
Const Ptrn As String = "??##?##"

LastRow = [A65536].End(xlUp).Row

For i = LastRow To FirstRow Step -1
Temp = Cells(i, 1).Text

If Not Temp Like Ptrn Then
Cells(i, 1).EntireRow.Delete
End If
Next i

End Sub
======================
--ron
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
Cross match data in Col A v/s Col B and display match in Col 3 aquaflow Excel Discussion (Misc queries) 3 July 10th 08 05:07 PM
remove #NA from index match formula. JBW Excel Worksheet Functions 2 February 1st 08 10:21 AM
remove records when fields match in 2 worksheets?? [email protected] Excel Worksheet Functions 6 May 9th 07 02:59 PM
How do I Remove Rows that match a criteria Mahesh Excel Discussion (Misc queries) 3 April 30th 07 10:58 PM
Remove ' character from copied excel cell to match data Syndrome Excel Discussion (Misc queries) 9 March 7th 07 05:18 PM


All times are GMT +1. The time now is 10:59 PM.

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

About Us

"It's about Microsoft Excel"