Solved

Stuck in a loop

Posted on 2013-02-04
11
258 Views
Last Modified: 2013-02-05
Hello,

I'm stuck in a loop! What I'm trying to achieve is starting in column A row 1 check to see if the cell value is "Unit" if so stop the code if not delete the row and test the cell value again.


Private Sub DeluptoUnit()

Worksheets("import").Activate
Range("a1").Select
Do

If Not ActiveCell = "Unit" Then Rows.Delete
ActiveCell.Offset(1, 0).Select

Loop Until ActiveCell = "Unit"

End Sub

Open in new window

0
Comment
Question by:sq30
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 38853357
Do something like this. It's untested so bear with me.

Dim lngLastRow as Long
Dim lngRow As Long
lngLastRow = Range("A65536").End(xlUp).Row

For lngRow = lngLastRow to 1 Step -1 ' Going backward is important
    If Cells(lngRow, 1).Value = "Unit" Then
        Rows(lngRow).EntireRow.delete
   End If
Next

Open in new window

0
 

Author Comment

by:sq30
ID: 38853383
Hi,

Thanks for the reply but I want it to do the opposite;

Test cell A1 for the word unit, if true stop the script if false delete row and test again.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 38853386
Then change line 6 to

If Cells(lngRow, 1).Value <> "Unit" Then
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sq30
ID: 38853443
Doesn't work it deletes all rows except the row with unit in
0
 
LVL 16

Expert Comment

by:terencino
ID: 38853480
Hi sq30, try this it uses AutoFilter to filter out the cells with Unit in them then deletes the rest
...Terry
Sub DeleteNonUnits()
  With ActiveSheet.UsedRange
    .AutoFilter Field:=1, Criteria1:="<>Unit"
    .Rows("2:" & .SpecialCells(xlCellTypeLastCell).Row).Delete Shift:=xlUp
    .AutoFilter
  End With
End Sub

Open in new window

0
 

Author Comment

by:sq30
ID: 38853500
Hi Terry

Thats deleting too much data also. I have a large batch of data the word unit can be found only once in column A.

I want the code to delete rows in sequence until if finds the ford "unit" once this is found STOP do not delete anymore rows.

e.g
Column A rows 1-271 has data <> "unit" delete these rows
row 272 = "unit" preserve this row
row273 onwards preserve these rows even if they do not = "unit"


That's why I was trying to tell my code to loop unit it finds the word unit.
0
 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 38853550
Dim Unit As Range
Dim lngRow As Long

Set Unit = Cells.Find(What:="Unit", After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
For lngRow = Unit.Row - 1 To 1 Step -1
        Rows(lngRow).EntireRow.Delete
Next

Open in new window

0
 
LVL 16

Accepted Solution

by:
terencino earned 250 total points
ID: 38853567
Right, Martin's has a great approach using Find. Here is some shortened code, which deletes all the rows prior:
Sub DeleteNonUnits()
   ActiveSheet.Range("A1:A" & Cells.Find(What:="unit", LookAt:=xlWhole).Row - 1).EntireRow.Delete
End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 38853902
Hi

Option Explicit

Sub kTest()
    
    Dim f, a As String
    
    a = Intersect(ActiveSheet.UsedRange, ActiveSheet.Range("a:a")).Address(external:=1)
    
    f = Evaluate("match(""unit""," & a & ",0)")
    If Not IsError(f) Then
        If f > 1 Then
            Range("A1:A" & f - 1).EntireRow.Delete
        End If
    End If
    
End Sub

Open in new window


Kris
0
 

Author Closing Comment

by:sq30
ID: 38854179
Spoiled for choice now :D - Thanks both. Terry's code is slightly fast on a sheet with 2k+ rows.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 38854872
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question