PNG32 PNG32 PNG32 PNG32
PNG32
Forum Excel, VBA, VSTO, Exceltips, Excelhj�lp PNG32 drivs av Excelspecialisten    Logga in     English
PNG32
PNG32 PNG32
PNG32

Logga in

PNG32

Du är inte inloggad. Logga in eller registrera dig för att skriva inlägg eller svara på inlägg.

För frågor om forumet, kontakta oss på webmaster@excelforum.se

PNG32 PNG32
PNG32 PNG32
PNG32

Excelforum

PNG32

 
ForumForumDiskussionerDiskussionerVBAVBAError: Macro to delete rows wit a certain valueError: Macro to delete rows wit a certain value
Föregående Föregående
 
Nästa Nästa
Nytt inlägg
 2013-08-16 13:35
 

I want to delete rows with the value "unique" in column c. For that i have written the following macro. However when i run the macro it deletes a majority of the rows but not all. If i run it a second time it deletes some more but i have to run it a couple of times for it to delete all of them. Could someone please explain why or how to modify the macro to work properly.

Thank you.

Sub DeleteUnique()

Dim Rng2 As Range
Dim SecRng As Range

Set SecRng = Range(Range("c2"), Range("c" & Rows.Count).End(xlUp))
For Each Rng2 In SecRng
If Rng2.Value = "unique" Then
Rng2.EntireRow.Delete
End If
On Error Resume Next
Next Rng2


End Sub

Nytt inlägg
 2013-09-19 07:37
 

You problem is caused by deleting rows top to bottom. Try the other way round instead i.e.

Sub DelUnique()

Dim i As Integer

For i = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1

If Cells(i, 3) = "unique" Then

Rows(i).EntireRow.Delete

End If

Next

End Sub

Depending on how much data you have another option would be to use "Autofilter" with "unique" as criteria and then delete all the rows that matches in one go.

Alf

Nytt inlägg
 2013-09-19 07:37
 
 Ändrad av Alf  på 2013-09-19 07:39:43

Deleted as this was a duplicate message

Föregående Föregående
 
Nästa Nästa
ForumForumDiskussionerDiskussionerVBAVBAError: Macro to delete rows wit a certain valueError: Macro to delete rows wit a certain value

PNG32 PNG32
Excelforum drivs av Excelspecialisten som bedriver utbildning i Excel och VBA, tillhandahåller support och hjälp med Excel, utvecklar program i Excel. Är ni i behov av en konsult inom Excel, VBA eller VSTO, eller söker en excelkurs, kontakta oss.
Copyright 2013 ExcelSpecialisten XLS AB   Användarvillkor  Personliga uppgifter