Solved

Stuck in a loop

Posted on 2013-02-04
11
221 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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
Then change line 6 to

If Cells(lngRow, 1).Value <> "Unit" Then
0
 

Author Comment

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

Expert Comment

by:terencino
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:sq30
Comment Utility
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 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Spoiled for choice now :D - Thanks both. Terry's code is slightly fast on a sheet with 2k+ rows.
0
 
LVL 45

Expert Comment

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

Marty - MVP 2009 to 2012
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now