Line Breaks


Line Breaks

Your code can also be made more readable and easier to work with by inserting line breaks in the middle of long lines of code. In VBA, if you are going to split a line up, you need to add a space followed by an underscore ( _) just before the line break. This tells the VBA compiler that the current line of code continues on the following line.The following example shows how simple line breaks can be used to make long lines of code much easier to read and understand. Consider the following line of code:
If (index = 1 And sColor1 = "red") Or (index = 2 And sColor1 = "blue") Or (index = 3 And sColor1 = "green") Or (index = 4And sColor1 = "brown") Then

By adding line breaks the code can be presented as follows:
If (index = 1 And sColor1 = "red") Or _
   (index = 2 And sColor1 = "blue") Or _
   (index = 3 And sColor1 = "green") Or _
   (index = 4 And sColor1 = "brown") Then

The above presentation enables you to see the different conditions within the 'If' statement much more clearly and will therefore assist you in producing, and maintaining, effective bug-free code.

Comments

Popular posts from this blog

50 Excel VBA Oral Interview Questions