PC Review


Reply
Thread Tools Rate Thread

delete rows for values

 
 
Jake
Guest
Posts: n/a
 
      9th Dec 2008
Hello,
I have code that I hoped would iterate through a column and delete rows if
values did not begin with a 7.

For Each rngDBSingleCostElemCell In wksSheet.Range("C2:C" &
lCostElemItemLastRow)
If Left(rngDBSingleCostElemCell, 1) <> 7 Then
rngDBSingleCostElemCell.Rows.Delete
End If
Next rngDBSingleCostElemCell

The values in column C are numbers stored as text.
For some reason rows with values like 16101, 16151 remain.
thanks for any help.
Jake
 
Reply With Quote
 
 
 
 
Eduardo
Guest
Posts: n/a
 
      9th Dec 2008

Hi,
Highlight column C and apply text to columns

"Jake" wrote:

> Hello,
> I have code that I hoped would iterate through a column and delete rows if
> values did not begin with a 7.
>
> For Each rngDBSingleCostElemCell In wksSheet.Range("C2:C" &
> lCostElemItemLastRow)
> If Left(rngDBSingleCostElemCell, 1) <> 7 Then
> rngDBSingleCostElemCell.Rows.Delete
> End If
> Next rngDBSingleCostElemCell
>
> The values in column C are numbers stored as text.
> For some reason rows with values like 16101, 16151 remain.
> thanks for any help.
> Jake

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      9th Dec 2008

Doesn't that only work for cells formatted as General in which the number is
preceded with an apostrophe? I think cells formatted as Text will remain as
Text after doing what you suggested.

--
Rick (MVP - Excel)


"Eduardo" <(E-Mail Removed)> wrote in message
news:7015D7B2-3A98-4701-AC65-(E-Mail Removed)...
> Hi,
> Highlight column C and apply text to columns
>
> "Jake" wrote:
>
>> Hello,
>> I have code that I hoped would iterate through a column and delete rows
>> if
>> values did not begin with a 7.
>>
>> For Each rngDBSingleCostElemCell In wksSheet.Range("C2:C" &
>> lCostElemItemLastRow)
>> If Left(rngDBSingleCostElemCell, 1) <> 7 Then
>> rngDBSingleCostElemCell.Rows.Delete
>> End If
>> Next rngDBSingleCostElemCell
>>
>> The values in column C are numbers stored as text.
>> For some reason rows with values like 16101, 16151 remain.
>> thanks for any help.
>> Jake


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      9th Dec 2008

I think you are falling victim to the removals that are taking place. When
you delete Row 1, all the other rows move up so that what was Row 2 is not
Row 1, etc. If Row2 had 16101 in it, that value would be in Row 1 after the
original Row 1 was deleted; but the For..Next loop already processed Row 1,
so it will not go back to it again. The way most people avoid this problem
is to iterate the loop row-by-row from the last row with data backwards to
the first row; that way, deletions do not affect the loop's processing
order. Try this (off the top of my head)...

With wksSheet
For X = lCostElemItemLastRow To 2 Step -1
If Left(.Cells(X, 1).Value, 1) <> 7 Then .Cells(X, 1).EntireRow.Delete
Next
End With

--
Rick (MVP - Excel)


"Jake" <(E-Mail Removed)> wrote in message
news:F15F320A-EFBE-4332-B67A-(E-Mail Removed)...
> Hello,
> I have code that I hoped would iterate through a column and delete rows if
> values did not begin with a 7.
>
> For Each rngDBSingleCostElemCell In wksSheet.Range("C2:C" &
> lCostElemItemLastRow)
> If Left(rngDBSingleCostElemCell, 1) <> 7 Then
> rngDBSingleCostElemCell.Rows.Delete
> End If
> Next rngDBSingleCostElemCell
>
> The values in column C are numbers stored as text.
> For some reason rows with values like 16101, 16151 remain.
> thanks for any help.
> Jake


 
Reply With Quote
 
Jake
Guest
Posts: n/a
 
      9th Dec 2008

Thanks Rick! Workin' great now.

"Rick Rothstein" wrote:

> I think you are falling victim to the removals that are taking place. When
> you delete Row 1, all the other rows move up so that what was Row 2 is not
> Row 1, etc. If Row2 had 16101 in it, that value would be in Row 1 after the
> original Row 1 was deleted; but the For..Next loop already processed Row 1,
> so it will not go back to it again. The way most people avoid this problem
> is to iterate the loop row-by-row from the last row with data backwards to
> the first row; that way, deletions do not affect the loop's processing
> order. Try this (off the top of my head)...
>
> With wksSheet
> For X = lCostElemItemLastRow To 2 Step -1
> If Left(.Cells(X, 1).Value, 1) <> 7 Then .Cells(X, 1).EntireRow.Delete
> Next
> End With
>
> --
> Rick (MVP - Excel)
>
>
> "Jake" <(E-Mail Removed)> wrote in message
> news:F15F320A-EFBE-4332-B67A-(E-Mail Removed)...
> > Hello,
> > I have code that I hoped would iterate through a column and delete rows if
> > values did not begin with a 7.
> >
> > For Each rngDBSingleCostElemCell In wksSheet.Range("C2:C" &
> > lCostElemItemLastRow)
> > If Left(rngDBSingleCostElemCell, 1) <> 7 Then
> > rngDBSingleCostElemCell.Rows.Delete
> > End If
> > Next rngDBSingleCostElemCell
> >
> > The values in column C are numbers stored as text.
> > For some reason rows with values like 16101, 16151 remain.
> > thanks for any help.
> > Jake

>
>

 
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
Retain rows with Max values - Delete other rows Raj Microsoft Excel Programming 2 26th Dec 2008 02:59 PM
How to Delete empty rows in excel in b/w rows with values =?Utf-8?B?RGVubmlz?= Microsoft Excel Worksheet Functions 3 28th Aug 2007 04:15 PM
delete rows with 0 values =?Utf-8?B?YW5kcmVzZzE5NzU=?= Microsoft Excel Programming 1 29th Sep 2006 03:49 PM
Delete Rows where there are #N/A Values FIRSTROUNDKO via OfficeKB.com Microsoft Excel Worksheet Functions 3 3rd Aug 2006 04:03 PM
Delete rows with numeric values, leave rows with text =?Utf-8?B?R1NwbGluZQ==?= Microsoft Excel Programming 5 11th Oct 2005 12:44 AM


Features
 

Advertising
 

Newsgroups
 


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