1

I can't seem to get my second for loop right. I'm looking for the cell with value 'Persoonlijke prijslijst'. Once I have this cell I need to go up two and delete 8 down. When I debug, it says temp = 0 so I presume it's in my second for loop.

Dim i As Integer
For i = 1 To 800
    Range("C" & i).Select
    If Range("C" & i).Value = "Persoonlijke prijslijst" Then
        Dim temp As Integer
        For temp = i - 2 To temp + 8
            Range("C" & temp).EntireRow.Delete Shift:=xlToLeft
        Next temp
    End If
Next i
4
  • 1
    Do you mean go up two rows and then delete 8 rows down from there? Commented Mar 14, 2012 at 15:03
  • It's because you're deleting rows within that second for loop. Every time a row is deleted, the temp variable would also need to be updated. But it's better to do it Kyle's way. Commented Mar 14, 2012 at 15:27
  • It's the thought that counts :) Commented Mar 14, 2012 at 15:37
  • How come one can have incorrect thoughts ;) Commented Mar 14, 2012 at 15:38

2 Answers 2

5

Another way without looping 800 times:

 Sub testing()

Dim rng As Range
Dim fAddress As String
Dim rngRows As Range

With Sheet1.Range("C1:C800")
    Set rng = .Find("Persoonlijke prijslijst")
    If Not rng Is Nothing Then
        fAddress = rng.Address
        Do
        If rngRows Is Nothing Then
            Set rngRows = rows(rng.Row - 2 & ":" & rng.Row + 5)
        Else
            Set rngRows = Union(rngRows, rows(rng.Row - 2 & ":" & rng.Row + 5))
        End If
           Set rng = .FindNext(rng)
        Loop While Not rng Is Nothing And rng.Address <> fAddress
    End If
End With

rngRows.EntireRow.Delete

End Sub
Sign up to request clarification or add additional context in comments.

12 Comments

Does this work for multiple selections? So if Persoonlijke prijslijst is in my document 4 times?
@Tom, I have covered about .Find and .FindNext in this link. "siddharthrout.wordpress.com/2011/07/14/…" For such small number of rows, you can use looping. However if you use .Find you have to be very careful as if you delete the range then Set rng = .FindNext(rng) line will give you the error ;) Let me know if you want a .Find code as well :)
@SiddharthRout, so using .find and .findnext is less CPU intensive? I haven't used this function in the past, so I'm quite interested. If you have time and don't mind, I would say yes to a .find version :)
@SiddharthRout is quite right, I hadn't tested it. I've updated my original code :S Thanks Sid
@Tom, Find is fast, but it also requires less looping, you should try and avoid looping in VBA since it is slow. Calling the worksheet (doing things with it) is also slow so should be done as little as possible or all in one go if you can
|
4

Is this what you are trying?

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim StrSearch As String
    Dim i As Long

    '~~> Change this to the relevant sheet name        
    Set ws = Sheets("Sheet1")

    StrSearch = "Persoonlijke prijslijst"

    With ws
        For i = 800 To 1 Step -1
            If .Range("C" & i).Value = StrSearch Then
                .Rows(i - 2 & ":" & i + 5).Delete
            End If
        Next i
    End With
End Sub

2 Comments

@mattboy: It's not about points. I never care for that ;) So you may reverse the vote that you gave. It about doing something right ;)
@Siddhart: Nah you still deserve it. Yours probably actually works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.