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.

Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.