VBA If statement tip

When creating long VBA code it is common that the start of an If statement and the matching End If statement may not be visible on the same screen.

When scrolling around trying to understand your code it can be useful to include the If statement itself as a comment following on the same line as the End If command – see examples below.

If x=0 Then
 
  'lots of code
 
  If y =1 Then
 
    'lots of code
 
  End If   'If y =1 then
 
End If   'If x=0 Then

The apostrophe is used to specify the start of a comment – you can have a comment following a line of code.

This structure can assist when trying to identify which End If statement relates to which If statement.