I'm writing a VBA problem which finds lines which are considered to be invalid, saves the row number in an array called invalidRowsToDelete
and deletes the selected rows by building a string of the invalid rows (e.g. 1:1, 4:4, 7:7
).
However, it sometimes works, it sometimes doesn't, but fails more consistently with large amounts of invalid rows.
The error it returns is: "Runtime error '1004': Method range of object '_global' failed" on the line Set rng = Range(invalidRowsToDelete)
Public rng As Range
__________________________
Dim invalidRowsToDelete As String
Dim i As Long
For i = LBound(InvalidFilesArr) To UBound(InvalidFilesArr)
If InvalidFilesArr(i) <> "" Then
invalidRowsToDelete = invalidRowsToDelete & InvalidFilesArr(i) & ":" & InvalidFilesArr(i) & ","
Else
Exit For
End If
Next i
'Build range statement and delete trailing comma from rowsToDeleteStatement
invalidRowsToDelete = "" & Left(invalidRowsToDelete, Len(invalidRowsToDelete) - 1) & ""
Debug.Print invalidRowsToDelete
Worksheets("Sheet1").Activate
Set rng = Range(invalidRowsToDelete) #### Problem line
rng.Select
rng.Delete
This has had me for quite a while now and I can't work out the reason for it causing this error.
Thanks
range
method. – Degustaf