Article ID: 185583
Article Last Modified on 10/10/2006
-or-
Worksheets.Add (Worksheets(1))Because parentheses are used around the argument, it is dereferenced; the Value property of the Worksheet object is passed to the Add method rather than the Worksheet object itself. The following line does not generate an error because the argument is not enclosed in parentheses and, therefore, the Worksheet object is not dereferenced:
Worksheets.Add Worksheets(1)
Sub AddWorksheet()
Worksheets.Add (Worksheets(1)) ' -- This line generates error
End Sub
When this macro is run, the run-time error '438' is generated. When
Microsoft Excel attempts to dereference "Worksheets(1)", a macro error
occurs because the Worksheet object does not support the Value property.
Sub Main()
GetRangeValue (Range("Sheet1!A1"))
End Sub
Sub GetRangeValue (x)
MsgBox x.Value ' -- This line generates error
End Sub
When this macro is run, the run-time error '424' is generated. Microsoft
Excel successfully dereferences the Range object for "Sheet1!A1" and passes
the Value property of that Range object to the GetRangeValue procedure. The
variable that is passed to GetRangeValue is not an object variable;
instead, it could be a String or a Double, depending on the contents of the
cell Sheet1!A1. The MsgBox line then fails because "x" is not an object
variable.
Sub Test()
MsgBox TypeName(Range("A1")) ' -- NOT Dereferenced
MsgBox TypeName((Range("A1"))) ' -- Dereferenced
End Sub
When you run this macro, the first MsgBox returns "Range" as the type of
the variable and the second MsgBox returns either "Double" or "String,"
depending on the contents of cell A1 in the active worksheet.
120802 Office: How to Add/Remove a Single Office Program or Component
181058 OFF98: How to Run Sample Code from Knowledge Base Articles
Additional query words: XL98
Keywords: kberrmsg kbprb KB185583