I find myself executing macros from python, but I have one in particular that has given me work, which throws me error 91, at one point I noticed that not finding "#N/D" throws me this error, so I put a conditional before executing it to validate if it is empty, however it still throws me this error when executing it.
This is my code:
Sub EliminarCancelados()
'
' EliminarCancelados Macro
'
Sheets("Exportar").Select
Dim Borrar As Range
Dim i As Long
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
With ThisWorkbook.Sheets("Exportar")
For i = 1 To .UsedRange.Rows.Count + 1
If .Cells(i, "K").Text = "#N/D" Then
If Borrar Is Nothing Then
Set Borrar = .Cells(i, 1)
Else
Set Borrar = Union(Borrar, .Cells(i, 1))
End If
End If
Next i
End With
If Not IsEmpty(Borrar) Then
Borrar.EntireRow.Delete
End If
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
and here is a screenshot of the error.
It seems to me that what you want is to delete all rows that result in an error in column K. You can greatly simplify your code by taking advantage of SpecialCells , which allows you to select multiple cells at once that meet a specific condition (give error on Your case):
If your errors in column K come from formulas, try:
If your errors are already values and are no longer formulas then:
Font: