Syntax errors

All programmers are likely to encounter syntax errors at some time or another. Syntax errors arise during the writing of the code as a result of grammatical, punctuation or spelling mistakes. In VBA, a line containing a syntax error will be shown in red, highlighting the offending word or line item. The programmer must correct the error before the code can be run. An example is shown in Figure 8.1. This syntax error has arisen because the programmer has failed to include a space between the keywords If and Not.

Worksheets ( "Weeklysales") .Select ActiveSheet.Unprotect Dim cell A3 Object

Foe Each cell In Range ("IJeek_sales")

If Not IsNumeric(cell)

MsgEox "Please Enter a nuinber in cellos cell, Address With cell,Interior . Color Index = .3

End With End If Next

ActiveSheet.Protect End Sub

Figure 8.1 The syntax error dialog box

End With End If Next

ActiveSheet.Protect End Sub

Figure 8.1 The syntax error dialog box

There are many other examples where this type or error could occur. For example, failing to include an end quote when referring to a named range, or making a spelling mistake in a VBA keyword by writing something like 'InputtBox' instead of' InputBox'. To correct a syntax error, you must know the correct syntax. Use the built-in on-line Help and the line syntax checker to help resolve syntax errors.

0 0

Post a comment