PC Review


Reply
Thread Tools Rate Thread

Delete row if column A cell text length is not 11

 
 
John Keith
Guest
Posts: n/a
 
      30th Sep 2008
I want to loop through the data in column A and if the text length of
the contents of the cell is not equal to 11 I want to delete the row.

Here's what I have that is not working (error is function not
compatible with object or something to that effect):

For i = Range("A:A").Rows.Count To 1 Step -1
If WorksheetFunction.Len(Range("A:A").Rows(i)) <> 11 Then
Range("A:A").Rows(i).EntireRow.Delete
End If
Next i

Can someone point out what I'm doing wrong, or suggest a better
method?

Thanks




John Keith
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
imageswords.br@gmail.com
Guest
Posts: n/a
 
      30th Sep 2008
Hi John,
this should do the trick.
You dont need to invoke the worksheet function to return the length.
Also you where testing the length of the Row
"Len(Range("A:A").Rows(i)) <> 11" when you should be testing the
length of the value of the cell.
I think this is where you were going wrong.
Let me know if you have a problem

Kind regards
Bernie

-----------------------------------------------------------------------


Public Sub DeleteTheRows()
Dim i As Double 'Cant use and integer because it cant hold the value
65536.

With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
For i = Worksheets(1).Range("A:A").Rows.Count To 1 Step -1
If Len(Range("A:A").Cells(i)) <> 11 Then
Range("A:A").Cells(i).EntireRow.Delete
End If
Next i
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
 
Reply With Quote
 
John Keith
Guest
Posts: n/a
 
      30th Sep 2008
On Mon, 29 Sep 2008 22:57:52 -0700 (PDT), (E-Mail Removed)
wrote:

>Hi John,
>this should do the trick.
>You dont need to invoke the worksheet function to return the length.
>Also you where testing the length of the Row
>"Len(Range("A:A").Rows(i)) <> 11" when you should be testing the
>length of the value of the cell.
>I think this is where you were going wrong.
>Let me know if you have a problem


Bernie,

Perfect! Thank you very much.

Let me ask a couple of questions (I'm still learning).

When I use the ".Calculation" I get a compile error but all is OK when
I remove the ".", what's going on with that?

I have understood that not all worksheet functions are available in a
macro using the same name so that I thought that is why the
"WorksheetFunction" was used. Obviously that is not the case with the
LEN function. How do I know when I have to use the worksheet function?

Finally, what is your suggestion on the best way to become more
proficient with excel macros beyond the hands on experiments I'm doing
now?

Thanks again!


John Keith
(E-Mail Removed)
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      30th Sep 2008

Dim LastRow as long
dim i As long
With activesheet
'just look at the rows that are used
lastrow = .cells(.rows.count,"A").end(xlup).row

For i = lastrow to 1 Step -1
If len(.cells(i,"A").value) <> 11 then
.Rows(i).Delete
End If
Next i
End with



John Keith wrote:
>
> I want to loop through the data in column A and if the text length of
> the contents of the cell is not equal to 11 I want to delete the row.
>
> Here's what I have that is not working (error is function not
> compatible with object or something to that effect):
>
> For i = Range("A:A").Rows.Count To 1 Step -1
> If WorksheetFunction.Len(Range("A:A").Rows(i)) <> 11 Then
> Range("A:A").Rows(i).EntireRow.Delete
> End If
> Next i
>
> Can someone point out what I'm doing wrong, or suggest a better
> method?
>
> Thanks
>
>
>
> John Keith
> (E-Mail Removed)


--

Dave Peterson
 
Reply With Quote
 
John Keith
Guest
Posts: n/a
 
      1st Oct 2008
On Tue, 30 Sep 2008 07:53:26 -0500, Dave Peterson
<(E-Mail Removed)> wrote:

>
>Dim LastRow as long
>dim i As long
>With activesheet
> 'just look at the rows that are used
> lastrow = .cells(.rows.count,"A").end(xlup).row
>
> For i = lastrow to 1 Step -1
> If len(.cells(i,"A").value) <> 11 then
> .Rows(i).Delete
> End If
> Next i
>End with



Dave,

Thank you for your solution. It has given me a key for another problem
I need to solve.


John Keith
(E-Mail Removed)
 
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
Text to column fixed length Luci Microsoft Excel Misc 6 27th Nov 2008 06:27 PM
Summing a column based on the length of text in a cell in anothercolumn nughty@gmail.com Microsoft Excel Worksheet Functions 13 26th Nov 2008 05:42 PM
Search a Column by text length =?Utf-8?B?a2JfNjM=?= Microsoft Excel Worksheet Functions 2 6th May 2005 09:17 PM
Maximum Length of Text in Column Joe Microsoft Excel Discussion 5 18th Dec 2004 03:47 PM
Re: How access length of text column? Graham Mandeno Microsoft Access Form Coding 0 21st Jul 2004 02:14 AM


Features
 

Advertising
 

Newsgroups
 


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