Based on your comment, you are seeing an Application-defined or object-defined error
when your Endcolumn
variable is 0. This is happening because the Excel Range
is 1-based, not 0-based, which means there is never a column 0.
Since you seem to be most interested in the error handling specifically, here's roughly how it should be handled:
Sub ErrorExample()
On Error GoTo ErrHandler ' Set the Error Handling Condition
' in this case, if an error occurs
' goto the ErrHandler label
' do stuff
Debug.Print "I am in `do stuff` code"
Range(Worksheets("Search Engine").Cells(9, 1),
Worksheets("Search Engine").Cells(Endcolumn,
Endrow + 2)).Select Selection.RowHeight = 20
Exit Sub ' Exit from the Sub gracefully and do not run the
' following lines of code (if this is not
' included, the ErrHandler code will run in all
' cases, not just error cases
Debug.Print "I will never run"
ErrHandler:
Debug.Print "I am in the error code"
' Code to run in case of error
ThisWorkbook.Worksheets("Search Engine").Protect ' protect your sheet
On Error GoTo 0 ' Reset the error handling condition
End Sub