Delete method of Range class failed - HELP!

R

richilli

Hi

Any help on this would be appreciated cos its driving me insane.

I have a function in VB.NET that takes in an excel range and tries to
delete rows where the first column starts with a string. Only it
doesnt work and all i get is "Delete method of Range class failed"
whatever i do.

Any suggestions?

Public Function DeleteRowsContaining(ByVal objRange As
Excel.Range, ByVal strDeleteString As String) As Excel.Range

Dim lngRow As Long
Dim strCellValue As String

For lngRow = objRange.Rows.Count To 1 Step -1
strCellValue = objRange.Cells(lngRow, 1).Value
If strCellValue.StartsWith(strDeleteString) = True Then
objRange.Rows(lngRow).EntireRow.Delete()
End If
Next lngRow

Return objRange

End Function

Many thanks in advance

R
 
G

Guest

You might have to delcare the worksheet as the active sheet before using the
delete range.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top