XL: Can't Set Value Property in For Each with Variant Type

ID: Q107903

The information in this article applies to:

SUMMARY

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.

MORE INFORMATION

Microsoft 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/refguide/

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
      Next

      ' 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
      Next

   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.

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.

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.

REFERENCES

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

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: 8.0
Keywords          : kbprg kbdta kbdtacode KbVBA 
Version           : WINDOWS: 5.0,5.0a,5.0c,7.0,7.0a,97; MACINTOSH: 5.0,5.0a
Platform          : MACINTOSH WINDOWS
Issue type        : kbprb

Last Reviewed: May 17, 1999