Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stuck in a loop

Posted on 2013-02-04
11
Medium Priority
?
266 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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 49

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 49

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 49

Assisted Solution

by:Martin Liss
Martin Liss earned 1000 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 1000 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 49

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

916 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