Microsoft KB Archive/191176

= XL98: Value Property Unchanged in For Each Loop with Variant Type =

Article ID: 191176

Article Last Modified on 9/12/2006

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q191176





SYMPTOMS
When you use a For Each loop in a Visual Basic for Applications macro, the macro may run without error, but values changed in the For Each loop may not actually change.



CAUSE
Values changed in a For Each loop may only appear to be changed during the running of the macro if the following conditions are true:


 * You are changing the default value property of an object or an element in an array. -and-


 * The loop control variable is a Variant data type.

This behavior is by design of Microsoft Excel.



WORKAROUND
To work around this problem, use either of the following methods:


 * Use the Dim statement to declare the control variable of type Object rather than a Variant data type. -or-


 * Use an explicit default value property in your macro code.



MORE INFORMATION
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. To demonstrate the issue described earlier, follow these steps:

 Save and close all open workbooks and then open a new workbook. Enter the following in Sheet1:

A1: 1

A2: 2

A3: 3

 Start the Visual Basic Editor (press OPTION+F11). On the Insert menu, click Module.  Enter the following code in the module: ' In this macro, cellRange is an Object variable ' and cellItem is a Variant variable.

Sub LoopTest

Set cellRange = ActiveSheet.Range("A1:A3")

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

End Sub </li> Run the LoopTest macro.

The first three message boxes display the values in A1:A3 concatenated with the letter A. However, the next three message boxes display the original values in A1:A3, and when the macro is finished, the values in A1:A3 are unchanged.</li>  Add the following line to the macro just below the Sub LoopTest line: Dim cellItem As Object </li> Rerun the LoopTest macro.

The first and second set of message boxes display the values in A1:A3 concatenated with the letter A. When the macro is finished, the values in A1:A3 are also changed accordingly.</li></ol>

NOTE: To work around this issue, you can also change the line of code cellItem = cellItem & "A"

to:

cellItem.Value = cellItem & "A"

<div class="references_section">