Microsoft KB Archive/107903

From BetaArchive Wiki

Article ID: 107903

Article Last Modified on 10/10/2006


  • Microsoft Excel 97 Standard Edition

This article was previously published under Q107903


The 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.


Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. The 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

  ' In this macro, cellRange is an Object variable
   ' and cellItem is a Variant variable.

   Sub loopTest()

      Set cellRange = ActiveSheet.Range(Selection.Address)

      ' The following Concatenates an "A" to the default value of each
      ' cell.
      For Each cellItem In cellRange
         cellItem = cellItem & "A"
         MsgBox cellItem

      ' The cellRange will return the default value (not value&"A"), even
      ' though the loop above changed the value of the object.
      For Each cellItem In cellRange
         MsgBox "n = " & cellItem

   End Sub

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.


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.

If you are working with an array, there is no workaround: when you use a For Each loop with an array, you have read-only access. Although you do not receive an error message if you attempt to write to an array, the array element will not be written to.


For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

163435 VBA: Programming Resources for Visual Basic for Applications

Additional query words: 8.0 XL

Keywords: kbdtacode kbprb kbprogramming KB107903