Solved

Stuck in a loop

Posted on 2013-02-04
11
261 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: 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

Suggested Solutions

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,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

710 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