Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 504
Default If Or not working

I get a type mismatch error on the code below at the IF line. Im trying to
pick out the rows that start with a certain text and delete all of the other
rows to shift cells up. I have it working now by selecting each cell and
evaluating it but it takes a long time to go through 1 or 2 thousand lines.
I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four and
Five, I just want the One and Two rows to stay. Can anyone offer any
suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default If Or not working

Roedd <<Kevin wedi ysgrifennu:

I get a type mismatch error on the code below at the IF line. Im
trying to pick out the rows that start with a certain text and delete
all of the other rows to shift cells up. I have it working now by
selecting each cell and evaluating it but it takes a long time to go
through 1 or 2 thousand lines. I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four
and Five, I just want the One and Two rows to stay. Can anyone offer
any suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin


Each clause of the or needs to be a complete test:

If Left(ActiveCell, 3) < "One" Or Left(ActiveCell, 3) < "Two" Then

However, I think you really need an AND!

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default If Or not working

Dim c As Range, MyRange As Range
For Each c In MyRange
If Left(c, 3) < "One" Or Left(c, 3) < "Two" Then
c.EntireRow.Delete
End If
Next

Mike F
"Kevin" wrote in message
...
I get a type mismatch error on the code below at the IF line. Im trying to
pick out the rows that start with a certain text and delete all of the
other
rows to shift cells up. I have it working now by selecting each cell and
evaluating it but it takes a long time to go through 1 or 2 thousand
lines.
I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four and
Five, I just want the One and Two rows to stay. Can anyone offer any
suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default If Or not working

Yeah, if you write the If statement like that, you might as well take
it out, because it will always evaluate to true.

If what you're wanting is for the code run if the cell isn't equal to
"One" or "Two", then use:
If Left(ActiveCell, 3) < "One" And Left(ActiveCell, 3) < "Two" Then


Robert Bruce wrote:
Roedd <<Kevin wedi ysgrifennu:

I get a type mismatch error on the code below at the IF line. Im
trying to pick out the rows that start with a certain text and delete
all of the other rows to shift cells up. I have it working now by
selecting each cell and evaluating it but it takes a long time to go
through 1 or 2 thousand lines. I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four
and Five, I just want the One and Two rows to stay. Can anyone offer
any suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin


Each clause of the or needs to be a complete test:

If Left(ActiveCell, 3) < "One" Or Left(ActiveCell, 3) < "Two" Then

However, I think you really need an AND!

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default If Or not working

another approach

Sub find_val()
Dim myrange As Range
Set myrange = Worksheets(1).Range("a1:a50") '
For Each cell In myrange
Select Case Left(cell.Value, 3)
Case "One"
Case "Two"
Case Else
cell.Select
Selection.EntireRow.Delete

End Select
Next
End Sub

Kevin wrote:
I get a type mismatch error on the code below at the IF line. Im trying to
pick out the rows that start with a certain text and delete all of the other
rows to shift cells up. I have it working now by selecting each cell and
evaluating it but it takes a long time to go through 1 or 2 thousand lines.
I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four and
Five, I just want the One and Two rows to stay. Can anyone offer any
suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default If Or not working

Your next discovery will be that you are skipping rows.

you need to loop backward to use the approach you have chosen


assume you are making your check on the values in column C

Dim i as LOng, lastrow as Long
lastrow = cells(rows.count,3).End(xlup).Row
for i = lastrow to 2 step -1
s = lcase(left(cells(i,3),3))
if s < "one" and s < "two" then
rows(i).Delete
end if
Next

--
Regards,
Tom Ogilvy



"stevebriz" wrote:

another approach

Sub find_val()
Dim myrange As Range
Set myrange = Worksheets(1).Range("a1:a50") '
For Each cell In myrange
Select Case Left(cell.Value, 3)
Case "One"
Case "Two"
Case Else
cell.Select
Selection.EntireRow.Delete

End Select
Next
End Sub

Kevin wrote:
I get a type mismatch error on the code below at the IF line. Im trying to
pick out the rows that start with a certain text and delete all of the other
rows to shift cells up. I have it working now by selecting each cell and
evaluating it but it takes a long time to go through 1 or 2 thousand lines.
I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four and
Five, I just want the One and Two rows to stay. Can anyone offer any
suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 504
Default If Or not working

Thanks a bunch the AND statement did the trick. Although it doesnt run any
faster.

Is there a way to do a replace function and have it delete the rows of
unwanted text?

"John Fuller" wrote:

Yeah, if you write the If statement like that, you might as well take
it out, because it will always evaluate to true.

If what you're wanting is for the code run if the cell isn't equal to
"One" or "Two", then use:
If Left(ActiveCell, 3) < "One" And Left(ActiveCell, 3) < "Two" Then


Robert Bruce wrote:
Roedd <<Kevin wedi ysgrifennu:

I get a type mismatch error on the code below at the IF line. Im
trying to pick out the rows that start with a certain text and delete
all of the other rows to shift cells up. I have it working now by
selecting each cell and evaluating it but it takes a long time to go
through 1 or 2 thousand lines. I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four
and Five, I just want the One and Two rows to stay. Can anyone offer
any suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin


Each clause of the or needs to be a complete test:

If Left(ActiveCell, 3) < "One" Or Left(ActiveCell, 3) < "Two" Then

However, I think you really need an AND!

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default If Or not working

I could use a dummy column like this
(this assumes you have a header row in row 1)

Sub ABC()
Dim rng As Range, rng1 As Range
Dim rng2 As Range
Set rng = Cells(2, "IV").End(xlToLeft)(1, 2)
Set rng1 = Cells(Rows.Count, rng.Column - 1).End(xlUp)(1, 2)
With Range(rng, rng1)
.Formula = "=if(and(left(A2,3)<""One"",Left(A2,3)<""Two""), na(),"""")"
On Error Resume Next
Set rng2 = .SpecialCells(xlFormulas, xlErrors)
End With
On Error GoTo 0
If Not rng2 Is Nothing Then
rng2.EntireRow.Delete
End If
End Sub

or you could use find to search for all the three's, then four's, then five's

or you could use the autofilter

--
Regards,
Tom Ogilvy




"Kevin" wrote:

Thanks a bunch the AND statement did the trick. Although it doesnt run any
faster.

Is there a way to do a replace function and have it delete the rows of
unwanted text?

"John Fuller" wrote:

Yeah, if you write the If statement like that, you might as well take
it out, because it will always evaluate to true.

If what you're wanting is for the code run if the cell isn't equal to
"One" or "Two", then use:
If Left(ActiveCell, 3) < "One" And Left(ActiveCell, 3) < "Two" Then


Robert Bruce wrote:
Roedd <<Kevin wedi ysgrifennu:

I get a type mismatch error on the code below at the IF line. Im
trying to pick out the rows that start with a certain text and delete
all of the other rows to shift cells up. I have it working now by
selecting each cell and evaluating it but it takes a long time to go
through 1 or 2 thousand lines. I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four
and Five, I just want the One and Two rows to stay. Can anyone offer
any suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin

Each clause of the or needs to be a complete test:

If Left(ActiveCell, 3) < "One" Or Left(ActiveCell, 3) < "Two" Then

However, I think you really need an AND!

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default If Or not working

Guess I should also remove that dummy column

Sub ABC()
Dim rng As Range, rng1 As Range
Dim rng2 As Range, col as Long
Set rng = Cells(2, "IV").End(xlToLeft)(1, 2)
col = rng.column
Set rng1 = Cells(Rows.Count, rng.Column - 1).End(xlUp)(1, 2)
With Range(rng, rng1)
.Formula = "=if(and(left(A2,3)<""One"",Left(A2,3)<""Two""), na(),"""")"
On Error Resume Next
Set rng2 = .SpecialCells(xlFormulas, xlErrors)
End With
On Error GoTo 0
If Not rng2 Is Nothing Then
rng2.EntireRow.Delete
End If
columns(col).ClearContents
End Sub

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote:

I could use a dummy column like this
(this assumes you have a header row in row 1)

Sub ABC()
Dim rng As Range, rng1 As Range
Dim rng2 As Range
Set rng = Cells(2, "IV").End(xlToLeft)(1, 2)
Set rng1 = Cells(Rows.Count, rng.Column - 1).End(xlUp)(1, 2)
With Range(rng, rng1)
.Formula = "=if(and(left(A2,3)<""One"",Left(A2,3)<""Two""), na(),"""")"
On Error Resume Next
Set rng2 = .SpecialCells(xlFormulas, xlErrors)
End With
On Error GoTo 0
If Not rng2 Is Nothing Then
rng2.EntireRow.Delete
End If
End Sub

or you could use find to search for all the three's, then four's, then five's

or you could use the autofilter

--
Regards,
Tom Ogilvy




"Kevin" wrote:

Thanks a bunch the AND statement did the trick. Although it doesnt run any
faster.

Is there a way to do a replace function and have it delete the rows of
unwanted text?

"John Fuller" wrote:

Yeah, if you write the If statement like that, you might as well take
it out, because it will always evaluate to true.

If what you're wanting is for the code run if the cell isn't equal to
"One" or "Two", then use:
If Left(ActiveCell, 3) < "One" And Left(ActiveCell, 3) < "Two" Then


Robert Bruce wrote:
Roedd <<Kevin wedi ysgrifennu:

I get a type mismatch error on the code below at the IF line. Im
trying to pick out the rows that start with a certain text and delete
all of the other rows to shift cells up. I have it working now by
selecting each cell and evaluating it but it takes a long time to go
through 1 or 2 thousand lines. I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four
and Five, I just want the One and Two rows to stay. Can anyone offer
any suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin

Each clause of the or needs to be a complete test:

If Left(ActiveCell, 3) < "One" Or Left(ActiveCell, 3) < "Two" Then

However, I think you really need an AND!

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 504
Default If Or not working

This work great Tom!!! cut the time down a lot.

Thanks much for the help.

"Tom Ogilvy" wrote:

Guess I should also remove that dummy column

Sub ABC()
Dim rng As Range, rng1 As Range
Dim rng2 As Range, col as Long
Set rng = Cells(2, "IV").End(xlToLeft)(1, 2)
col = rng.column
Set rng1 = Cells(Rows.Count, rng.Column - 1).End(xlUp)(1, 2)
With Range(rng, rng1)
.Formula = "=if(and(left(A2,3)<""One"",Left(A2,3)<""Two""), na(),"""")"
On Error Resume Next
Set rng2 = .SpecialCells(xlFormulas, xlErrors)
End With
On Error GoTo 0
If Not rng2 Is Nothing Then
rng2.EntireRow.Delete
End If
columns(col).ClearContents
End Sub

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote:

I could use a dummy column like this
(this assumes you have a header row in row 1)

Sub ABC()
Dim rng As Range, rng1 As Range
Dim rng2 As Range
Set rng = Cells(2, "IV").End(xlToLeft)(1, 2)
Set rng1 = Cells(Rows.Count, rng.Column - 1).End(xlUp)(1, 2)
With Range(rng, rng1)
.Formula = "=if(and(left(A2,3)<""One"",Left(A2,3)<""Two""), na(),"""")"
On Error Resume Next
Set rng2 = .SpecialCells(xlFormulas, xlErrors)
End With
On Error GoTo 0
If Not rng2 Is Nothing Then
rng2.EntireRow.Delete
End If
End Sub

or you could use find to search for all the three's, then four's, then five's

or you could use the autofilter

--
Regards,
Tom Ogilvy




"Kevin" wrote:

Thanks a bunch the AND statement did the trick. Although it doesnt run any
faster.

Is there a way to do a replace function and have it delete the rows of
unwanted text?

"John Fuller" wrote:

Yeah, if you write the If statement like that, you might as well take
it out, because it will always evaluate to true.

If what you're wanting is for the code run if the cell isn't equal to
"One" or "Two", then use:
If Left(ActiveCell, 3) < "One" And Left(ActiveCell, 3) < "Two" Then


Robert Bruce wrote:
Roedd <<Kevin wedi ysgrifennu:

I get a type mismatch error on the code below at the IF line. Im
trying to pick out the rows that start with a certain text and delete
all of the other rows to shift cells up. I have it working now by
selecting each cell and evaluating it but it takes a long time to go
through 1 or 2 thousand lines. I'm trying to speed it up a little.
EX. If the first cell in each row starts with One, Two, Three, Four
and Five, I just want the One and Two rows to stay. Can anyone offer
any suggestions?
======================================
For Each c In MyRange
If Left(ActiveCell, 3) < "One" Or "Two" Then
ActiveCell.EntireRow.Delete
End If
Next
======================================
Thanks,
Kevin

Each clause of the or needs to be a complete test:

If Left(ActiveCell, 3) < "One" Or Left(ActiveCell, 3) < "Two" Then

However, I think you really need an AND!

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.


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
Calculate working days but change working week SamB Excel Discussion (Misc queries) 1 September 1st 08 09:17 PM
Making weekend days working days - the system cuts the working tim Fluffy Excel Discussion (Misc queries) 1 May 30th 08 10:02 PM
macro was working, now it's not working RichardO[_11_] Excel Programming 2 June 9th 04 06:27 AM
Macro working in Excel 2003; not working in Excel 2000 Leslie Barberie Excel Programming 5 May 20th 04 07:51 PM
Adding sales from a non working day to the previous working day Alex Excel Programming 1 September 19th 03 08:48 AM


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

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"