Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Macro to delete only certain rows

I am looking for a macro that will automatically delete all rows in a
worksheet where the value in column E is "Data". Any suggestions/help?
Thanks.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Macro to delete only certain rows

Record a macro when you apply data|filter|autofilter to column E. Then filter
to show only Data. Then delete those visible rows. Remove the autofilter and
stop recording the macro.

MikeCM wrote:

I am looking for a macro that will automatically delete all rows in a
worksheet where the value in column E is "Data". Any suggestions/help?
Thanks.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Macro to delete only certain rows


Thanks for the suggestion. Actually I was thinking more along the lines
of somethings such as (which I got from a similar discussion a while
back):

Option explicit
sub delete_()
dim myCell as range
dim myRng as range
dim delRng as range


with activesheet
set myrng = .range("a2", .cells(.rows.count,"A").end(xlup))
end with


for each mycell in myrng.cells
select case lcase(mycell.value)
case "Data"
'do nothing
case else
if delrng is nothing then
set delrng = mycell
else
set delrng = union(mycell, delrng)
end if
end select
next mycell


if delrng is nothing then
'do nothing
else
delrng.entirerow.delete
end if


end sub

The problem is that this deletes every row. I can't work out why
though. Any thoughts?

Mike


Dave Peterson wrote:
Record a macro when you apply data|filter|autofilter to column E. Then filter
to show only Data. Then delete those visible rows. Remove the autofilter and
stop recording the macro.

MikeCM wrote:

I am looking for a macro that will automatically delete all rows in a
worksheet where the value in column E is "Data". Any suggestions/help?
Thanks.


--

Dave Peterson


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Macro to delete only certain rows

Mike maybe something like this,

Sub Delete_Data_In_E()
Dim Rng As Range
Dim FindString As String
Dim RngFound As Boolean
FindString = "Data"
Application.ScreenUpdating = False
Do
Set Rng = Range("E:E").Find(What:=FindString, LookAt:=xlWhole)
If Not Rng Is Nothing Then Rng.EntireRow.Delete: RngFound = True
Loop While Not (Rng Is Nothing)

Application.ScreenUpdating = True
If Not RngFound Then MsgBox FindString & " not found"

End Sub

--
Paul B
Always backup your data before trying something new
Please post any response to the newsgroups so others can benefit from it
Feedback on answers is always appreciated!
Using Excel 2002 & 2003

"MikeCM" wrote in message
oups.com...

Thanks for the suggestion. Actually I was thinking more along the lines
of somethings such as (which I got from a similar discussion a while
back):

Option explicit
sub delete_()
dim myCell as range
dim myRng as range
dim delRng as range


with activesheet
set myrng = .range("a2", .cells(.rows.count,"A").end(xlup))
end with


for each mycell in myrng.cells
select case lcase(mycell.value)
case "Data"
'do nothing
case else
if delrng is nothing then
set delrng = mycell
else
set delrng = union(mycell, delrng)
end if
end select
next mycell


if delrng is nothing then
'do nothing
else
delrng.entirerow.delete
end if


end sub

The problem is that this deletes every row. I can't work out why
though. Any thoughts?

Mike


Dave Peterson wrote:
Record a macro when you apply data|filter|autofilter to column E. Then
filter
to show only Data. Then delete those visible rows. Remove the
autofilter and
stop recording the macro.

MikeCM wrote:

I am looking for a macro that will automatically delete all rows in a
worksheet where the value in column E is "Data". Any suggestions/help?
Thanks.


--

Dave Peterson




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Macro to delete only certain rows

First, comparing a lcase(mycell.value) to "Data" (with the upper case D) will
cause unintended problems. That comparison will never be true.

Second, I thought you wanted to delete the rows with Data in them. You're
keeping those rows and deleting the rest (under the case Else portion)

Third, the code looks at column A, not column E.

How about:

Option explicit
sub delete_()
dim myCell as range
dim myRng as range
dim delRng as range

with activesheet
set myrng = .range("e2", .cells(.rows.count,"e").end(xlup))
end with

for each mycell in myrng.cells
select case lcase(mycell.value)
case lcase("Data")
if delrng is nothing then
set delrng = mycell
else
set delrng = union(mycell, delrng)
end if
end select
next mycell

if delrng is nothing then
'do nothing
else
delrng.entirerow.delete
end if

end sub

ps. It's usually better to copy from the VBE and paste into your message. It's
just too easy to inject errors other ways (retyping???).



MikeCM wrote:

Thanks for the suggestion. Actually I was thinking more along the lines
of somethings such as (which I got from a similar discussion a while
back):

Option explicit
sub delete_()
dim myCell as range
dim myRng as range
dim delRng as range

with activesheet
set myrng = .range("a2", .cells(.rows.count,"A").end(xlup))
end with

for each mycell in myrng.cells
select case lcase(mycell.value)
case "Data"
'do nothing
case else
if delrng is nothing then
set delrng = mycell
else
set delrng = union(mycell, delrng)
end if
end select
next mycell

if delrng is nothing then
'do nothing
else
delrng.entirerow.delete
end if

end sub

The problem is that this deletes every row. I can't work out why
though. Any thoughts?

Mike

Dave Peterson wrote:
Record a macro when you apply data|filter|autofilter to column E. Then filter
to show only Data. Then delete those visible rows. Remove the autofilter and
stop recording the macro.

MikeCM wrote:

I am looking for a macro that will automatically delete all rows in a
worksheet where the value in column E is "Data". Any suggestions/help?
Thanks.


--

Dave Peterson


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Macro to delete only certain rows

Dave/Paul - thanks to both of you for your thoughts, I've got it to
work, plus also learned something as a result. Much appreciated!

Mike


Dave Peterson wrote:
First, comparing a lcase(mycell.value) to "Data" (with the upper case D) will
cause unintended problems. That comparison will never be true.

Second, I thought you wanted to delete the rows with Data in them. You're
keeping those rows and deleting the rest (under the case Else portion)

Third, the code looks at column A, not column E.

How about:

Option explicit
sub delete_()
dim myCell as range
dim myRng as range
dim delRng as range

with activesheet
set myrng = .range("e2", .cells(.rows.count,"e").end(xlup))
end with

for each mycell in myrng.cells
select case lcase(mycell.value)
case lcase("Data")
if delrng is nothing then
set delrng = mycell
else
set delrng = union(mycell, delrng)
end if
end select
next mycell

if delrng is nothing then
'do nothing
else
delrng.entirerow.delete
end if

end sub

ps. It's usually better to copy from the VBE and paste into your message. It's
just too easy to inject errors other ways (retyping???).



MikeCM wrote:

Thanks for the suggestion. Actually I was thinking more along the lines
of somethings such as (which I got from a similar discussion a while
back):

Option explicit
sub delete_()
dim myCell as range
dim myRng as range
dim delRng as range

with activesheet
set myrng = .range("a2", .cells(.rows.count,"A").end(xlup))
end with

for each mycell in myrng.cells
select case lcase(mycell.value)
case "Data"
'do nothing
case else
if delrng is nothing then
set delrng = mycell
else
set delrng = union(mycell, delrng)
end if
end select
next mycell

if delrng is nothing then
'do nothing
else
delrng.entirerow.delete
end if

end sub

The problem is that this deletes every row. I can't work out why
though. Any thoughts?

Mike

Dave Peterson wrote:
Record a macro when you apply data|filter|autofilter to column E. Then filter
to show only Data. Then delete those visible rows. Remove the autofilter and
stop recording the macro.

MikeCM wrote:

I am looking for a macro that will automatically delete all rows in a
worksheet where the value in column E is "Data". Any suggestions/help?
Thanks.

--

Dave Peterson


--

Dave Peterson


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
Macro to delete rows wilko Excel Discussion (Misc queries) 3 July 23rd 09 08:13 PM
My Macro Won't Delete Rows?? VexedFist New Users to Excel 3 April 16th 07 04:14 PM
macro to delete entire rows when column A is blank ...a quick macro vikram Excel Programming 4 May 3rd 04 08:45 PM
Macro to Delete Rows Ed[_14_] Excel Programming 4 February 24th 04 06:42 PM


All times are GMT +1. The time now is 09:49 PM.

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"