Solved

Stuck in a loop

Posted on 2013-02-04
11
249 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 46

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 46

Expert Comment

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

If Cells(lngRow, 1).Value <> "Unit" Then
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 46

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 46

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

840 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