I got code below. I am trying to learn about error handling in excel VBA.
Sub LoopErrorHandling()
Dim ws As Worksheet
Dim c As Range
On Error GoTo LoopErrorHandling_Err
Set ws = ThisWorkbook.Worksheets(1)
ws.Range("C1:C5").ClearContents
For Each c In ws.Range("A1:A5").Cells
c.Offset(0, 2).Value = c.Value / c.Offset(0, 1).Value
Next c
LoopErrorHandling_Exit:
On Error Resume Next
Set ws = Nothing
On Error GoTo 0
Exit Sub
LoopErrorHandling_Err:
MsgBox Err.Description
Resume Next
Resume LoopErrorHandling_Exit
End Sub
I want to understant the following in the above code.
- Should line
Set ws = Nothingbe coming after or before the lineLoopErrorHandling_Exit:. - Shouldn't line
LoopErrorHandling_Err:be enough, isLoopErrorHandling_Exit:necessary. - What is the work of line
LoopErrorHandling_Exit:in above code and does it triggers only if Error occurs. - Does above code covers everything what error handling needs in excel vba or is there stuff missing.

Set ws = Nothingshouldn't really be there at all..LoopErrorHandling_Exitis neccessary in this snippet (because there is noExit Subanywhere before the labels).LoopErrorHandling_Exit:is thelabelto which code will jump when theLoopErrorHandling_Exitis triggered or when normal code execution reaches it which is after the for each loop because there is notExit SubResume Nextstatement would make theResume LoopErrorHandling_Exitline pointless as it will never execute.