Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Deleting rows within a named range

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Deleting rows within a named range

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default Deleting rows within a named range

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default Deleting rows within a named range

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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Deleting rows within a named range

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
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
Selecting and deleting named range based on cell value Rich Kniatt[_5_] Excel Programming 1 April 6th 06 08:04 PM
how do i maintain named range addressing after deleting row? erp Excel Programming 0 December 1st 05 01:52 AM
How to add rows(cells) to a named range Ctech[_12_] Excel Programming 4 October 6th 05 03:54 PM
Deleting named ranges by looping through range collection agarwaldvk[_11_] Excel Programming 3 August 3rd 04 01:00 AM
Deleting a named range in VBA Jako[_19_] Excel Programming 4 June 13th 04 09:31 PM


All times are GMT +1. The time now is 11:22 AM.

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"