PC Review


Reply
Thread Tools Rate Thread

Deleting rows within a named range

 
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      9th May 2007
I have the following code

For Each r In myRange
If LCase(oWS.Name) = "status" Then
Debug.Print r.Address, myRange.Address
If r.Text <> fNameNew Then
r.EntireRow.Delete
End If
End If
Next r


Let's say myRange.address = B5:B23

If row 6 is deleted, the next r.address that's reviewed is B7, not a repeat
of B6. What do I need to do to fix this?

Thanks,
Barb Reinhardt
 
Reply With Quote
 
 
 
 
=?Utf-8?B?R2FyeScncyBTdHVkZW50?=
Guest
Posts: n/a
 
      9th May 2007
Don't delete rows within the For loop. Instead, build a new range in the
loop and delete afterwards

Sub dural()
Dim rr As Range
Set rr = Range("A65536")
For Each r In myRange
If LCase(oWS.Name) = "status" Then
Debug.Print r.Address, myRange.Address
If r.Text <> fNameNew Then
Set rr = Union(rr, r)
End If
End If
Next r
rr.EntireRow.Delete
End Sub

--
Gary''s Student - gsnu200719


"Barb Reinhardt" wrote:

> I have the following code
>
> For Each r In myRange
> If LCase(oWS.Name) = "status" Then
> Debug.Print r.Address, myRange.Address
> If r.Text <> fNameNew Then
> r.EntireRow.Delete
> End If
> End If
> Next r
>
>
> Let's say myRange.address = B5:B23
>
> If row 6 is deleted, the next r.address that's reviewed is B7, not a repeat
> of B6. What do I need to do to fix this?
>
> Thanks,
> Barb Reinhardt

 
Reply With Quote
 
JW
Guest
Posts: n/a
 
      9th May 2007
You could iterate through your for loop using an integer and the count
of items in your range.
Something like:
Sub tester()
Dim myRange As Range, r As Range
Dim i As Integer
Set myRange = Range("B5:B23")
For i = 1 To myRange.Count
For Each r In myRange
If r.Text = "status" Then
r.EntireRow.Delete
i = i - 1
End If
Next r
Next i
End Sub

Of course, I am programatically creating the myRange dimension, but a
named range should work the same way.

HTH
-Jeff-

Barb Reinhardt wrote:
> I have the following code
>
> For Each r In myRange
> If LCase(oWS.Name) = "status" Then
> Debug.Print r.Address, myRange.Address
> If r.Text <> fNameNew Then
> r.EntireRow.Delete
> End If
> End If
> Next r
>
>
> Let's say myRange.address = B5:B23
>
> If row 6 is deleted, the next r.address that's reviewed is B7, not a repeat
> of B6. What do I need to do to fix this?
>
> Thanks,
> Barb Reinhardt


 
Reply With Quote
 
JW
Guest
Posts: n/a
 
      9th May 2007
Oops. I forgot to add that I changed the code around a bot for
testing purposes. You would ned something like this:
Sub tester()
Dim myRange As Range, r As Range
Dim i As Integer
Set myRange = Range("B5:B23")
For i = 1 To myRange.Count
For Each r In myRange
If LCase(oWS.Name) = "status" Then
Debug.Print r.Address, myRange.Address
If r.Text <> fNameNew Then
r.EntireRow.Delete
End If
End If
Next r
Next i
End Sub

JW wrote:
> You could iterate through your for loop using an integer and the count
> of items in your range.
> Something like:
> Sub tester()
> Dim myRange As Range, r As Range
> Dim i As Integer
> Set myRange = Range("B5:B23")
> For i = 1 To myRange.Count
> For Each r In myRange
> If r.Text = "status" Then
> r.EntireRow.Delete
> i = i - 1
> End If
> Next r
> Next i
> End Sub
>
> Of course, I am programatically creating the myRange dimension, but a
> named range should work the same way.
>
> HTH
> -Jeff-
>
> Barb Reinhardt wrote:
> > I have the following code
> >
> > For Each r In myRange
> > If LCase(oWS.Name) = "status" Then
> > Debug.Print r.Address, myRange.Address
> > If r.Text <> fNameNew Then
> > r.EntireRow.Delete
> > End If
> > End If
> > Next r
> >
> >
> > Let's say myRange.address = B5:B23
> >
> > If row 6 is deleted, the next r.address that's reviewed is B7, not a repeat
> > of B6. What do I need to do to fix this?
> >
> > Thanks,
> > Barb Reinhardt


 
Reply With Quote
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      9th May 2007
Thanks. I'll do that.

"Gary''s Student" wrote:

> Don't delete rows within the For loop. Instead, build a new range in the
> loop and delete afterwards
>
> Sub dural()
> Dim rr As Range
> Set rr = Range("A65536")
> For Each r In myRange
> If LCase(oWS.Name) = "status" Then
> Debug.Print r.Address, myRange.Address
> If r.Text <> fNameNew Then
> Set rr = Union(rr, r)
> End If
> End If
> Next r
> rr.EntireRow.Delete
> End Sub
>
> --
> Gary''s Student - gsnu200719
>
>
> "Barb Reinhardt" wrote:
>
> > I have the following code
> >
> > For Each r In myRange
> > If LCase(oWS.Name) = "status" Then
> > Debug.Print r.Address, myRange.Address
> > If r.Text <> fNameNew Then
> > r.EntireRow.Delete
> > End If
> > End If
> > Next r
> >
> >
> > Let's say myRange.address = B5:B23
> >
> > If row 6 is deleted, the next r.address that's reviewed is B7, not a repeat
> > of B6. What do I need to do to fix this?
> >
> > Thanks,
> > Barb Reinhardt

 
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
Rows in named range? IanC Microsoft Excel Programming 5 1st Dec 2009 09:25 AM
UDF: Count rows in named range John Microsoft Excel Programming 8 7th Jan 2009 05:34 PM
how do i maintain named range addressing after deleting row? =?Utf-8?B?ZXJw?= Microsoft Excel Programming 0 1st Dec 2005 01:52 AM
How to add rows(cells) to a named range Ctech Microsoft Excel Programming 4 6th Oct 2005 03:54 PM
Deleting a named range in VBA Jako Microsoft Excel Programming 5 13th Jun 2004 09:31 PM


Features
 

Advertising
 

Newsgroups
 


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