PDA

View Full Version : Run-Time Error '1004' Delete method of Range class failed



Denblanc
04-01-2016, 06:35 PM
Good evening to everyone,
This code works but needs tweeking and not sure how to accomplish. My worksheet has 1300 rows and this code reduces it to 145. The two problems are:
1. After running I get the following msgbox: "Run-Time Error '1004' Delete method of Range class failed". {line it stops at highlighted below}
2. The time it takes to complete task is a good 2 minutes - would like to speed it up.

Any assistance would be appreciated.



Sub SortingCountry_Rows()


Dim lr As Long, r As Long, L As Long, i As Long
Dim BlackFound As Boolean

Application.ScreenUpdating = False
lr = Range("AD" & Rows.Count).End(xlUp).row
For r = lr To 3 Step -1
BlackFound = False
i = 0
With Range("AD" & r)
L = Len(.Value)
Do
i = i + 1
If .Characters(i, 1).Font.ColorIndex = 1 Then
BlackFound = True
End If
Loop Until BlackFound = True Or i = L
If BlackFound Then
Rows(r).Delete ' Error occurs here
End If
End With
Next r
Application.ScreenUpdating = True

End Sub

SamT
04-01-2016, 08:09 PM
See how this works and if it's faster

With Range("AD" & r)
For i = 1 to .Characters.Count
If .Characters(i, 1).Font.ColorIndex = 1 Then
.EntireRow.Delete
Exit for
End If
Next
End With

snb
04-02-2016, 04:08 AM
Sub M_snb()
for each cl in columns(30).specialcells(2,2)
for j=1 to cl.characters.count
If .Characters(j, 1).Font.ColorIndex = 1 Then
cl=""
exit for
end if
next
next

columns(30).specialcells(4).entirerow.delete
End Sub

Denblanc
04-04-2016, 06:02 PM
To SamT, your code still gives me an "Error 1004 Delete method of Range Class failed" on the [.EntireRow.Delete] line.
To snb, your code gives me "Compile Error: invalid or unqualified reference" on the [If .Characters] line

Sorry for not responding until now, but thank you both for your suggestions.

snb
04-05-2016, 12:40 AM
It means you didn't analyse the code.
It's very(!) simple to adapt.