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 conditionally delete 3-row blocks in excel

I'm beating my head against a wall here, so I guess it's time to break down
and ask for help. I'm trying to write a macro that looks in each row in
column D on a spreadsheet, and if that cell contains "INFO", I want it to
delete that row as well as the row before it and after it. I have a macro
written that should delete that row, but i'm giving myself a headache trying
to get it to include the rows before and after it. Any ideas? Here's the
code I've started with...

sub DeleteHeaderRows

Dim rng As Range
Dim rngToDelete As Range
Dim rngToSearch As Range

With ActiveSheet
Set rngToSearch = .Range(.Range("D1"), .Cells(Rows.Count, "D").End(xlUp))
.DisplayPageBreaks = False
For Each rng In rngToSearch
If rng.Value = "INFO" Then
If rngToDelete Is Nothing Then
Set rngToDelete = rng
Else
Set rngToDelete = Union(rng, rngToDelete)
End If
End If
Next rng
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End With
End Sub

--
Marty
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default macro to conditionally delete 3-row blocks in excel

tricky stuff, xman............
from what i understand, you have to do this sort of thing backwards,
from the bottom up. because..........

ok, your macro has gotten to row 15, where it contains "INFO." it
deletes row 15. now row 15 used to be row 16. and if you delete row
14, well, i'm not sure now which row is which............... &
neither does your macro.
see what a mess it makes????
:)

my suggestion (& i'm no guru, & somebody will almost definitely have a
better [or more concise] idea than me) would be....

start at the bottom. i'll use column A. macro checks a15 - no
"info". macro moves to a14 - "info". macro offsets to row 15 in a
different cell & puts some sort of an indicator there, that this needs
to be deleted (later), like an "x". macro now deletes a14 & a13.
rows 14 & 13 disappear, still leaving you on row 15, which of course
is not the ORIGINAL row 15, but your macro counting has not messed
up. macro now proceeds to a14 - no "info".
etc.
then, when you're all done, search through the column where you have
the 'x's & delete any rows that have an x.

convoluted (probably waaaaaaaay too complicated - i have a horrible
habit of doing that), but i think it would work. worked in limited
manual testing, for me.

:)
susan



On Jun 1, 3:15 pm, Xman019 (donotspam) wrote:
I'm beating my head against a wall here, so I guess it's time to break down
and ask for help. I'm trying to write a macro that looks in each row in
column D on a spreadsheet, and if that cell contains "INFO", I want it to
delete that row as well as the row before it and after it. I have a macro
written that should delete that row, but i'm giving myself a headache trying
to get it to include the rows before and after it. Any ideas? Here's the
code I've started with...

sub DeleteHeaderRows

Dim rng As Range
Dim rngToDelete As Range
Dim rngToSearch As Range

With ActiveSheet
Set rngToSearch = .Range(.Range("D1"), .Cells(Rows.Count, "D").End(xlUp))
.DisplayPageBreaks = False
For Each rng In rngToSearch
If rng.Value = "INFO" Then
If rngToDelete Is Nothing Then
Set rngToDelete = rng
Else
Set rngToDelete = Union(rng, rngToDelete)
End If
End If
Next rng
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End With
End Sub

--
Marty



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default macro to conditionally delete 3-row blocks in excel

Hi Susan, I appreciate the quick response...

What you're describing is basically what my macro does (it works in the
spreadsheet i originally used it for, anyway)... instead of putting an 'x' in
another column it just saves the cell location it found 'info' in to a
string, then when it finds another one, it unions that location to the same
string. then when it's ready to delete, it selects all of the rows in the
string and deletes them all at once to avoid the row number confusion. The
problem is I can't figure out the language I need to use in order to have it
add not just the location of the cell with the 'info' in it, but also the
location of the cells above and below that cell.
Anyone have any ideas on how to do that?

Anyone... Anyone... Anyone...
Bueller... Bueller... Bueller
:)
--
Marty


"Susan" wrote:

tricky stuff, xman............
from what i understand, you have to do this sort of thing backwards,
from the bottom up. because..........

ok, your macro has gotten to row 15, where it contains "INFO." it
deletes row 15. now row 15 used to be row 16. and if you delete row
14, well, i'm not sure now which row is which............... &
neither does your macro.
see what a mess it makes????
:)

my suggestion (& i'm no guru, & somebody will almost definitely have a
better [or more concise] idea than me) would be....

start at the bottom. i'll use column A. macro checks a15 - no
"info". macro moves to a14 - "info". macro offsets to row 15 in a
different cell & puts some sort of an indicator there, that this needs
to be deleted (later), like an "x". macro now deletes a14 & a13.
rows 14 & 13 disappear, still leaving you on row 15, which of course
is not the ORIGINAL row 15, but your macro counting has not messed
up. macro now proceeds to a14 - no "info".
etc.
then, when you're all done, search through the column where you have
the 'x's & delete any rows that have an x.

convoluted (probably waaaaaaaay too complicated - i have a horrible
habit of doing that), but i think it would work. worked in limited
manual testing, for me.

:)
susan



On Jun 1, 3:15 pm, Xman019 (donotspam) wrote:
I'm beating my head against a wall here, so I guess it's time to break down
and ask for help. I'm trying to write a macro that looks in each row in
column D on a spreadsheet, and if that cell contains "INFO", I want it to
delete that row as well as the row before it and after it. I have a macro
written that should delete that row, but i'm giving myself a headache trying
to get it to include the rows before and after it. Any ideas? Here's the
code I've started with...

sub DeleteHeaderRows

Dim rng As Range
Dim rngToDelete As Range
Dim rngToSearch As Range

With ActiveSheet
Set rngToSearch = .Range(.Range("D1"), .Cells(Rows.Count, "D").End(xlUp))
.DisplayPageBreaks = False
For Each rng In rngToSearch
If rng.Value = "INFO" Then
If rngToDelete Is Nothing Then
Set rngToDelete = rng
Else
Set rngToDelete = Union(rng, rngToDelete)
End If
End If
Next rng
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End With
End Sub

--
Marty




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default macro to conditionally delete 3-row blocks in excel

Hi,

Does the following sub work as expected?

Sub DeleteHeaderRows()
Dim rng As Range, rngToSearch As Range
Application.ScreenUpdating = False
With ActiveSheet
Set rngToSearch = .Range(.Range("D1"), .Range("D65536").End(xlUp))
On Error Resume Next
Do
Set rng = rngToSearch.Find(what:="INFO", LookIn:=xlValues,
lookat:=xlWhole, MatchCase:=True)
Union(rng, rng.Offset(-1, 0), rng.Offset(1, 0)).Value =
CVErr(xlErrNA)
Loop While Not rng Is Nothing
rngToSearch.SpecialCells(xlCellTypeConstants,
xlErrors).EntireRow.Delete
End With
End Sub

Best regards from France,

Manu/


"Xman019" (donotspam) a écrit dans le message de news:
...
Hi Susan, I appreciate the quick response...

What you're describing is basically what my macro does (it works in the
spreadsheet i originally used it for, anyway)... instead of putting an 'x'
in
another column it just saves the cell location it found 'info' in to a
string, then when it finds another one, it unions that location to the
same
string. then when it's ready to delete, it selects all of the rows in the
string and deletes them all at once to avoid the row number confusion.
The
problem is I can't figure out the language I need to use in order to have
it
add not just the location of the cell with the 'info' in it, but also the
location of the cells above and below that cell.
Anyone have any ideas on how to do that?

Anyone... Anyone... Anyone...
Bueller... Bueller... Bueller
:)
--
Marty


"Susan" wrote:

tricky stuff, xman............
from what i understand, you have to do this sort of thing backwards,
from the bottom up. because..........

ok, your macro has gotten to row 15, where it contains "INFO." it
deletes row 15. now row 15 used to be row 16. and if you delete row
14, well, i'm not sure now which row is which............... &
neither does your macro.
see what a mess it makes????
:)

my suggestion (& i'm no guru, & somebody will almost definitely have a
better [or more concise] idea than me) would be....

start at the bottom. i'll use column A. macro checks a15 - no
"info". macro moves to a14 - "info". macro offsets to row 15 in a
different cell & puts some sort of an indicator there, that this needs
to be deleted (later), like an "x". macro now deletes a14 & a13.
rows 14 & 13 disappear, still leaving you on row 15, which of course
is not the ORIGINAL row 15, but your macro counting has not messed
up. macro now proceeds to a14 - no "info".
etc.
then, when you're all done, search through the column where you have
the 'x's & delete any rows that have an x.

convoluted (probably waaaaaaaay too complicated - i have a horrible
habit of doing that), but i think it would work. worked in limited
manual testing, for me.

:)
susan



On Jun 1, 3:15 pm, Xman019 (donotspam) wrote:
I'm beating my head against a wall here, so I guess it's time to break
down
and ask for help. I'm trying to write a macro that looks in each row
in
column D on a spreadsheet, and if that cell contains "INFO", I want it
to
delete that row as well as the row before it and after it. I have a
macro
written that should delete that row, but i'm giving myself a headache
trying
to get it to include the rows before and after it. Any ideas? Here's
the
code I've started with...

sub DeleteHeaderRows

Dim rng As Range
Dim rngToDelete As Range
Dim rngToSearch As Range

With ActiveSheet
Set rngToSearch = .Range(.Range("D1"), .Cells(Rows.Count,
"D").End(xlUp))
.DisplayPageBreaks = False
For Each rng In rngToSearch
If rng.Value = "INFO" Then
If rngToDelete Is Nothing Then
Set rngToDelete = rng
Else
Set rngToDelete = Union(rng, rngToDelete)
End If
End If
Next rng
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End With
End Sub

--
Marty






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default macro to conditionally delete 3-row blocks in excel

Hi Marty,

Try something like:

'============
Public Sub DeleteRange()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim rCell As Range
Dim delRng As Range
Dim iLastRow As Long
Dim CalcMode As Long
Const sstr As String = "Info"

Set WB = Workbooks("MyBook.xls") '<<=== CHANGE
Set SH = WB.Sheets("Sheet1") b '<<=== CHANGE

With SH
iLastRow = .Range("A" & Rows.Count).End(xlUp).Row
Set Rng = .Range("D1:D" & iLastRow)
End With

On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each rCell In Rng.Cells
With rCell
.Select
If StrComp(.Value, sstr, vbTextCompare) = 0 Then
If delRng Is Nothing Then
Set delRng = rCell.Offset(-1).Resize(3)
Else
Set delRng = Union(rCell.Offset(-1).Resize(3), delRng)
End If
End If
End With
Next rCell

If Not delRng Is Nothing Then
delRng.delate
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
'<<============


---
Regards,
Norman



"Xman019" (donotspam) wrote in message
...
I'm beating my head against a wall here, so I guess it's time to break
down
and ask for help. I'm trying to write a macro that looks in each row in
column D on a spreadsheet, and if that cell contains "INFO", I want it to
delete that row as well as the row before it and after it. I have a macro
written that should delete that row, but i'm giving myself a headache
trying
to get it to include the rows before and after it. Any ideas? Here's the
code I've started with...

sub DeleteHeaderRows

Dim rng As Range
Dim rngToDelete As Range
Dim rngToSearch As Range

With ActiveSheet
Set rngToSearch = .Range(.Range("D1"), .Cells(Rows.Count,
"D").End(xlUp))
.DisplayPageBreaks = False
For Each rng In rngToSearch
If rng.Value = "INFO" Then
If rngToDelete Is Nothing Then
Set rngToDelete = rng
Else
Set rngToDelete = Union(rng, rngToDelete)
End If
End If
Next rng
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End With
End Sub

--
Marty





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default macro to conditionally delete 3-row blocks in excel

Hi Marty,

Set SH = WB.Sheets("Sheet1") b '<<=== CHANGE


should read:

Set SH = WB.Sheets("Sheet1") '<<=== CHANGE



---
Regards,
Norman


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default macro to conditionally delete 3-row blocks in excel

Hi Marty,

Apologies - I forgot to remove a test line:

.Select


In 'real' code it is rarely desirable or efficient to make
selections and I should have deleted this line before
posting the code.



---
Regards,
Norman


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default macro to conditionally delete 3-row blocks in excel

Hi Marty,

And, as your intention is to delete entire rows, change:

delRng.delate


to

delRng.EntireRow.Delete

---
Regards,
Norman


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default macro to conditionally delete 3-row blocks in excel

Thanks to all of you for your help on this... I was able to get it to work
using tissot.emmanuel's code! As always, this site is a lifesaver.

--
Marty


"Norman Jones" wrote:

Hi Marty,

And, as your intention is to delete entire rows, change:

delRng.delate


to

delRng.EntireRow.Delete

---
Regards,
Norman



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
How do I conditionally delete rows based on cell contents? John Chan Excel Worksheet Functions 3 May 17th 23 03:45 AM
Conditionally delete James[_4_] Excel Discussion (Misc queries) 2 November 6th 07 05:55 PM
Can I conditionally delete row in Excel? (if B3=0 delete row B) Badger Excel Programming 4 January 23rd 06 01:21 AM
Conditionally Delete Cells From Named Range [email protected] Excel Programming 2 September 27th 04 06:21 PM
conditionally delete some elements from an array clui[_8_] Excel Programming 1 December 4th 03 01:21 AM


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