PC Review


Reply
Thread Tools Rate Thread

Decrementing row after deletion

 
 
=?Utf-8?B?U3VlQ29vbA==?=
Guest
Posts: n/a
 
      12th Jun 2007
I use the following code to delete a row containing a particular string, but
if two succesive rows contain the string, the second row is skipped. How can
I decrement the row counter, so that after a deletion the next for loop works
on the same row?

For Each C In myRange
C.Select
MyPos = InStr(1, C.Value, strWhat, 1)
If MyPos > 0 Then
Response = MsgBox("Delete?", vbYesNo + vbQuestion)
If Response = vbYes Then
Selection.EntireRow.Delete
End If
End If
Next

Any help would be much appreciated,

Sue

--
I''m not a complete idiot - there are some parts missing!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      12th Jun 2007
Do it in reverse to cure the problem

Sub deleteit()
lastrowcola = Range("A65536").End(xlUp).Row '<Alter to suit
For x = lastrowcola To 1 Step -1
Cells(x, 1).Select
MyPos = InStr(1, Cells(x, 1).Value, strWhat, 1)
If MyPos > 0 Then
Response = MsgBox("Delete?", vbYesNo + vbQuestion)
If Response = vbYes Then
Selection.EntireRow.Delete
End If
End If
Next

End Sub

Mike


"SueCool" wrote:

> I use the following code to delete a row containing a particular string, but
> if two succesive rows contain the string, the second row is skipped. How can
> I decrement the row counter, so that after a deletion the next for loop works
> on the same row?
>
> For Each C In myRange
> C.Select
> MyPos = InStr(1, C.Value, strWhat, 1)
> If MyPos > 0 Then
> Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> If Response = vbYes Then
> Selection.EntireRow.Delete
> End If
> End If
> Next
>
> Any help would be much appreciated,
>
> Sue
>
> --
> I''m not a complete idiot - there are some parts missing!

 
Reply With Quote
 
Vasant Nanavati
Guest
Posts: n/a
 
      12th Jun 2007
Try going backwards (also, no need to select anything):

Something like (untested):

Dim i As Long
For i = myRange.Rows.Count to 1 Step -1
MyPos = InStr(1, myRange(i).Valu, strWhat, 1)
If MyPos > 0 Then
Response = MsgBox("Delete?", vbYesNo + vbQuestion)
If Response = vbYes Then
myRange(i).EntireRow.Delete
End If
End If
Next

________________________________________________________________________



"SueCool" <(E-Mail Removed)> wrote in message
news:2E10B346-E99D-441A-AB69-(E-Mail Removed)...
>I use the following code to delete a row containing a particular string,
>but
> if two succesive rows contain the string, the second row is skipped. How
> can
> I decrement the row counter, so that after a deletion the next for loop
> works
> on the same row?
>
> For Each C In myRange
> C.Select
> MyPos = InStr(1, C.Value, strWhat, 1)
> If MyPos > 0 Then
> Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> If Response = vbYes Then
> Selection.EntireRow.Delete
> End If
> End If
> Next
>
> Any help would be much appreciated,
>
> Sue
>
> --
> I''m not a complete idiot - there are some parts missing!



 
Reply With Quote
 
=?Utf-8?B?U3VlQ29vbA==?=
Guest
Posts: n/a
 
      12th Jun 2007
Thanks Mike - that's done the job. Luckily the strings I am searching for
are in the first column, but how would I search all the cells in each row?

Sue
--
I''''m not a complete idiot - there are some parts missing!


"Mike H" wrote:

> Do it in reverse to cure the problem
>
> Sub deleteit()
> lastrowcola = Range("A65536").End(xlUp).Row '<Alter to suit
> For x = lastrowcola To 1 Step -1
> Cells(x, 1).Select
> MyPos = InStr(1, Cells(x, 1).Value, strWhat, 1)
> If MyPos > 0 Then
> Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> If Response = vbYes Then
> Selection.EntireRow.Delete
> End If
> End If
> Next
>
> End Sub
>
> Mike
>
>
> "SueCool" wrote:
>
> > I use the following code to delete a row containing a particular string, but
> > if two succesive rows contain the string, the second row is skipped. How can
> > I decrement the row counter, so that after a deletion the next for loop works
> > on the same row?
> >
> > For Each C In myRange
> > C.Select
> > MyPos = InStr(1, C.Value, strWhat, 1)
> > If MyPos > 0 Then
> > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > If Response = vbYes Then
> > Selection.EntireRow.Delete
> > End If
> > End If
> > Next
> >
> > Any help would be much appreciated,
> >
> > Sue
> >
> > --
> > I''m not a complete idiot - there are some parts missing!

 
Reply With Quote
 
=?Utf-8?B?U3VlQ29vbA==?=
Guest
Posts: n/a
 
      12th Jun 2007
Thanks Vasant - the logic seemed sound but something strange seemed to happen
when I tried it.

I've got a fix now, so thanks anyway,

Sue
--
I''''m not a complete idiot - there are some parts missing!


"Vasant Nanavati" wrote:

> Try going backwards (also, no need to select anything):
>
> Something like (untested):
>
> Dim i As Long
> For i = myRange.Rows.Count to 1 Step -1
> MyPos = InStr(1, myRange(i).Valu, strWhat, 1)
> If MyPos > 0 Then
> Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> If Response = vbYes Then
> myRange(i).EntireRow.Delete
> End If
> End If
> Next
>
> ________________________________________________________________________
>
>
>
> "SueCool" <(E-Mail Removed)> wrote in message
> news:2E10B346-E99D-441A-AB69-(E-Mail Removed)...
> >I use the following code to delete a row containing a particular string,
> >but
> > if two succesive rows contain the string, the second row is skipped. How
> > can
> > I decrement the row counter, so that after a deletion the next for loop
> > works
> > on the same row?
> >
> > For Each C In myRange
> > C.Select
> > MyPos = InStr(1, C.Value, strWhat, 1)
> > If MyPos > 0 Then
> > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > If Response = vbYes Then
> > Selection.EntireRow.Delete
> > End If
> > End If
> > Next
> >
> > Any help would be much appreciated,
> >
> > Sue
> >
> > --
> > I''m not a complete idiot - there are some parts missing!

>
>
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      12th Jun 2007
Sue,

Put another for/next loop in

Cells(x, 1).Select
for y = 1 to 256
MyPos = InStr(1, Cells(x, y).Value, strWhat, 1)
If MyPos > 0 Then
Response = MsgBox("Delete?", vbYesNo + vbQuestion)
If Response = vbYes Then
Selection.EntireRow.Delete
next y


Not tested but sjould work,

Mike
"SueCool" wrote:

> Thanks Mike - that's done the job. Luckily the strings I am searching for
> are in the first column, but how would I search all the cells in each row?
>
> Sue
> --
> I''''m not a complete idiot - there are some parts missing!
>
>
> "Mike H" wrote:
>
> > Do it in reverse to cure the problem
> >
> > Sub deleteit()
> > lastrowcola = Range("A65536").End(xlUp).Row '<Alter to suit
> > For x = lastrowcola To 1 Step -1
> > Cells(x, 1).Select
> > MyPos = InStr(1, Cells(x, 1).Value, strWhat, 1)
> > If MyPos > 0 Then
> > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > If Response = vbYes Then
> > Selection.EntireRow.Delete
> > End If
> > End If
> > Next
> >
> > End Sub
> >
> > Mike
> >
> >
> > "SueCool" wrote:
> >
> > > I use the following code to delete a row containing a particular string, but
> > > if two succesive rows contain the string, the second row is skipped. How can
> > > I decrement the row counter, so that after a deletion the next for loop works
> > > on the same row?
> > >
> > > For Each C In myRange
> > > C.Select
> > > MyPos = InStr(1, C.Value, strWhat, 1)
> > > If MyPos > 0 Then
> > > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > > If Response = vbYes Then
> > > Selection.EntireRow.Delete
> > > End If
> > > End If
> > > Next
> > >
> > > Any help would be much appreciated,
> > >
> > > Sue
> > >
> > > --
> > > I''m not a complete idiot - there are some parts missing!

 
Reply With Quote
 
=?Utf-8?B?U3VlQ29vbA==?=
Guest
Posts: n/a
 
      12th Jun 2007
Thanks Mike - you are a star, but that was a bit of an easy one!

Sue
--
I''''m not a complete idiot - there are some parts missing!


"Mike H" wrote:

> Sue,
>
> Put another for/next loop in
>
> Cells(x, 1).Select
> for y = 1 to 256
> MyPos = InStr(1, Cells(x, y).Value, strWhat, 1)
> If MyPos > 0 Then
> Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> If Response = vbYes Then
> Selection.EntireRow.Delete
> next y
>
>
> Not tested but sjould work,
>
> Mike
> "SueCool" wrote:
>
> > Thanks Mike - that's done the job. Luckily the strings I am searching for
> > are in the first column, but how would I search all the cells in each row?
> >
> > Sue
> > --
> > I''''m not a complete idiot - there are some parts missing!
> >
> >
> > "Mike H" wrote:
> >
> > > Do it in reverse to cure the problem
> > >
> > > Sub deleteit()
> > > lastrowcola = Range("A65536").End(xlUp).Row '<Alter to suit
> > > For x = lastrowcola To 1 Step -1
> > > Cells(x, 1).Select
> > > MyPos = InStr(1, Cells(x, 1).Value, strWhat, 1)
> > > If MyPos > 0 Then
> > > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > > If Response = vbYes Then
> > > Selection.EntireRow.Delete
> > > End If
> > > End If
> > > Next
> > >
> > > End Sub
> > >
> > > Mike
> > >
> > >
> > > "SueCool" wrote:
> > >
> > > > I use the following code to delete a row containing a particular string, but
> > > > if two succesive rows contain the string, the second row is skipped. How can
> > > > I decrement the row counter, so that after a deletion the next for loop works
> > > > on the same row?
> > > >
> > > > For Each C In myRange
> > > > C.Select
> > > > MyPos = InStr(1, C.Value, strWhat, 1)
> > > > If MyPos > 0 Then
> > > > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > > > If Response = vbYes Then
> > > > Selection.EntireRow.Delete
> > > > End If
> > > > End If
> > > > Next
> > > >
> > > > Any help would be much appreciated,
> > > >
> > > > Sue
> > > >
> > > > --
> > > > I''m not a complete idiot - there are some parts missing!

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      12th Jun 2007
You might want to exit your second loop when you delete the row.


--
Regards,
Tom Ogilvy


"SueCool" wrote:

> Thanks Mike - you are a star, but that was a bit of an easy one!
>
> Sue
> --
> I''''m not a complete idiot - there are some parts missing!
>
>
> "Mike H" wrote:
>
> > Sue,
> >
> > Put another for/next loop in
> >
> > Cells(x, 1).Select
> > for y = 1 to 256
> > MyPos = InStr(1, Cells(x, y).Value, strWhat, 1)
> > If MyPos > 0 Then
> > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > If Response = vbYes Then
> > Selection.EntireRow.Delete
> > next y
> >
> >
> > Not tested but sjould work,
> >
> > Mike
> > "SueCool" wrote:
> >
> > > Thanks Mike - that's done the job. Luckily the strings I am searching for
> > > are in the first column, but how would I search all the cells in each row?
> > >
> > > Sue
> > > --
> > > I''''m not a complete idiot - there are some parts missing!
> > >
> > >
> > > "Mike H" wrote:
> > >
> > > > Do it in reverse to cure the problem
> > > >
> > > > Sub deleteit()
> > > > lastrowcola = Range("A65536").End(xlUp).Row '<Alter to suit
> > > > For x = lastrowcola To 1 Step -1
> > > > Cells(x, 1).Select
> > > > MyPos = InStr(1, Cells(x, 1).Value, strWhat, 1)
> > > > If MyPos > 0 Then
> > > > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > > > If Response = vbYes Then
> > > > Selection.EntireRow.Delete
> > > > End If
> > > > End If
> > > > Next
> > > >
> > > > End Sub
> > > >
> > > > Mike
> > > >
> > > >
> > > > "SueCool" wrote:
> > > >
> > > > > I use the following code to delete a row containing a particular string, but
> > > > > if two succesive rows contain the string, the second row is skipped. How can
> > > > > I decrement the row counter, so that after a deletion the next for loop works
> > > > > on the same row?
> > > > >
> > > > > For Each C In myRange
> > > > > C.Select
> > > > > MyPos = InStr(1, C.Value, strWhat, 1)
> > > > > If MyPos > 0 Then
> > > > > Response = MsgBox("Delete?", vbYesNo + vbQuestion)
> > > > > If Response = vbYes Then
> > > > > Selection.EntireRow.Delete
> > > > > End If
> > > > > End If
> > > > > Next
> > > > >
> > > > > Any help would be much appreciated,
> > > > >
> > > > > Sue
> > > > >
> > > > > --
> > > > > I''m not a complete idiot - there are some parts missing!

 
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
Subtract two dates (decrementing daily) and add text Peter Microsoft Excel Worksheet Functions 4 29th Oct 2008 01:35 AM
decrementing numbers in inventory table =?Utf-8?B?Qmlubmll?= Microsoft Access VBA Modules 2 12th Jun 2007 02:30 PM
Incrementing/decrementing column characters using only worksheet functions? Dan Microsoft Excel Worksheet Functions 4 7th Oct 2004 12:34 AM
Incrementing/decrementing column characters using only worksheet functions? Dan Microsoft Excel Misc 2 7th Oct 2004 12:34 AM
Decrementing a text string Bralyan Microsoft Excel Worksheet Functions 0 27th Jun 2004 05:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:40 PM.