1
I have an Excel document with several sheets. In this document I need to execute a command on a specific sheet, that is by the sheet name. But if that sheet does not exist execute another command on another sheet with specific name 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 will give error Worksheets(sheets(i)). Activate! How to 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!
Thank you for your comment dot. Py!
– b8engl
Yes, but what if the sheet doesn’t exist. How do I fix the error and continue the loop?
– b8engl