I have an Excel document with multiple sheets. In this document I need to execute a command on a specific sheet, ie the sheet name. But if this sheet does not exist run another command on another specific named sheet until the sheets are finished.
Example:
Sub adicionar_categorias()
folhas = Array ("FOLHA 1", "FOLHA 2", "FOLHA 3")
For i = LBound(folhas) To UBound(folhas)
Worksheets(folhas(i)).Activate
Range("X2").Select
ActiveCell.FormulaR1C1 = "Computadores>Portáteis"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("X2").AutoFill Destination:=Range("X2:X" & Lastrow)
Next i
End Sub
If the sheet is not in the book it will give Worksheets (sheets (i)) error. ! How do I resolve the error?
RESOLVED: On Error Resume Next
Sub adicionar_categorias()
folhas = Array ("FOLHA 1", "FOLHA 2", "FOLHA 3")
On Error Resume Next
For i = LBound(folhas) To UBound(folhas)
Worksheets(folhas(i)).Activate
Range("X2").Select
ActiveCell.FormulaR1C1 = "Computadores>Portáteis"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("X2").AutoFill Destination:=Range("X2:X" & Lastrow)
Next i
End Sub
Thank you "dot.Py!