Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Macro to Delete Rows

I run this macro to delete unwanted rows of data that I import into excel.
If a value in
column B does not match "CR5673" the row is deleted. I now have more values
that
I want to include along with "CR5673". For example "DA2618" & "DA1131" do
not
need to be deleted.

Do I write three different if statements?

Also these values are dynamic. Can it compare the values in a defined named
range?

T.I.A.
Ed




Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If Not (Range("B" & i).Value Like "CR5673") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i


End Sub




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Macro to Delete Rows

Hi
try the following. Change the line
If Not (Range("B" & i).Value Like "CR5673") Then
to
If Not (Range("B" & i).Value Like "CR5673") _
and Not (Range("B" & i).Value Like "DA2618") _
and Not (Range("B" & i).Value Like "DA1131") _
Then

--
Regards
Frank Kabel
Frankfurt, Germany

Ed wrote:
I run this macro to delete unwanted rows of data that I import into
excel. If a value in
column B does not match "CR5673" the row is deleted. I now have more
values that
I want to include along with "CR5673". For example "DA2618" &
"DA1131" do not
need to be deleted.

Do I write three different if statements?

Also these values are dynamic. Can it compare the values in a defined
named range?

T.I.A.
Ed




Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If Not (Range("B" & i).Value Like "CR5673") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i


End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Macro to Delete Rows

Ed,

Untested, but

Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If (Not (Range("B" & i).Value Like "CR5673") And _
Not (Range("B" & i).Value Like "DA2618") And _
Not (Range("B" & i).Value Like "DA1131") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i

End Sub

To test againsta named range, then try


Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If (Not (Range("B" & i).Value Like Worksheets("Sheet1").Range("cr1"))
And _
Not (Range("B" & i).Value Like Worksheets("Sheet1").Range("da1"))
And _
Not (Range("B" & i).Value Like Worksheets("Sheet1").Range("da2"))
Then
Range("B" & i).EntireRow.Delete
End If
Next 'i

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Ed" wrote in message
...
I run this macro to delete unwanted rows of data that I import into excel.
If a value in
column B does not match "CR5673" the row is deleted. I now have more

values
that
I want to include along with "CR5673". For example "DA2618" & "DA1131" do
not
need to be deleted.

Do I write three different if statements?

Also these values are dynamic. Can it compare the values in a defined

named
range?

T.I.A.
Ed




Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If Not (Range("B" & i).Value Like "CR5673") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i


End Sub






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Macro to Delete Rows

Sorry, a missing ) before the Then on both versions.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Bob Phillips" wrote in message
...
Ed,

Untested, but

Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If (Not (Range("B" & i).Value Like "CR5673") And _
Not (Range("B" & i).Value Like "DA2618") And _
Not (Range("B" & i).Value Like "DA1131") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i

End Sub

To test againsta named range, then try


Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If (Not (Range("B" & i).Value Like Worksheets("Sheet1").Range("cr1"))
And _
Not (Range("B" & i).Value Like Worksheets("Sheet1").Range("da1"))
And _
Not (Range("B" & i).Value Like Worksheets("Sheet1").Range("da2"))
Then
Range("B" & i).EntireRow.Delete
End If
Next 'i

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Ed" wrote in message
...
I run this macro to delete unwanted rows of data that I import into

excel.
If a value in
column B does not match "CR5673" the row is deleted. I now have more

values
that
I want to include along with "CR5673". For example "DA2618" & "DA1131"

do
not
need to be deleted.

Do I write three different if statements?

Also these values are dynamic. Can it compare the values in a defined

named
range?

T.I.A.
Ed




Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If Not (Range("B" & i).Value Like "CR5673") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i


End Sub








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Macro to Delete Rows

Assume the list of values to be kept are in a defined range name named
KeepList (a range on a worksheet)

Dim LastRow As Long
Dim i As Long, bKeep as Boolean
Dim cell as Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
bKeep = False
for each cell in Range("KeepList")
If Ucase(Range("B" & i).Value) = Ucase(cell.value) Then
bKeep = True
Exit for
end if
Next
if not bKeep then
Range("B" & i).EntireRow.Delete
End If
Next i

--
Regards,
Tom Ogilvy


"Ed" wrote in message
...
I run this macro to delete unwanted rows of data that I import into excel.
If a value in
column B does not match "CR5673" the row is deleted. I now have more

values
that
I want to include along with "CR5673". For example "DA2618" & "DA1131" do
not
need to be deleted.

Do I write three different if statements?

Also these values are dynamic. Can it compare the values in a defined

named
range?

T.I.A.
Ed




Sub Step02()
'Delete rows with unwanted data

Dim LastRow As Long
Dim i As Long
LastRow = Range("A6536").End(xlUp).Row

For i = LastRow To 1 Step -1
If Not (Range("B" & i).Value Like "CR5673") Then
Range("B" & i).EntireRow.Delete
End If
Next 'i


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
Need a macro to delete rows jmr4h8 Excel Discussion (Misc queries) 9 July 2nd 08 11:16 PM
My Macro Won't Delete Rows?? VexedFist New Users to Excel 3 April 16th 07 04:14 PM
Delete all Rows Macro Wanna Learn Excel Discussion (Misc queries) 5 March 6th 07 10:06 PM
delete rows using macro nospam Excel Worksheet Functions 5 December 20th 06 01:26 PM
delete rows-macro TUNGANA KURMA RAJU Excel Discussion (Misc queries) 5 January 13th 06 12:01 PM


All times are GMT +1. The time now is 08:32 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"