The information in this article applies to:
SUMMARYThe default value property of an object or an element in an array cannot be set with a For Each loop if the loop control variable is a Variant data type. MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspThe following macro example uses a For Each loop to concatenate the letter "A" to the values in a range and then displays the results in a message box. The loop counter in this macro is defined with a Variant data type. When the loop attempts to change the value of the cells in the range by concatenating the variant loop counter, the routine appears to run properly and the new value appears in a message box as expected. However, when a second For Each loop redisplays the values in the range by calling the variant loop counter, the range values have reverted back to their original state (the range values no longer have the "A" concatenated to them). Macro Example
The connection between the object and the variant is broken when the loop
attempts to write to the value property of the object. The result of this
break is that the new value is assigned to the Variant variable, but the
object is not updated. When the first loop references the variant, the
Variant variable passes the value that it is holding (instead of getting
the object's value property) and the loop appears to work. However, when
the second loop references the Variant variable, it is passed the value
that the variable is reading from the object--which was never changed.
This behavior is by design. WORKAROUND
If you are working with a collection of objects, use an explicit value
property (cellItem.value=cellItem & "A") or use the Dim statement to
declare the control variable as an Object rather than a Variant data type.
REFERENCESFor additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base: Q163435 VBA: Programming Resources for Visual Basic for Applications Additional query words: 8.0
Keywords : kbprg kbdta kbdtacode KbVBA |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |