ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   = positioning (https://www.excelbanter.com/excel-programming/302732-%3D-positioning.html)

snax500[_2_]

= positioning
 
Why does it matter where you put the "=" in a macro (see line 3). I am range
valuing cells that contain the "If" formula. In the following code:

For Each cll In Range("v10:v44")
If Left(cll.Formula, 3) = "=IF" Then
cll.Value = cll
End If
Next

why does this range value my cells but this next code does not:

For Each cll In Range("v10:v44")
If Left(cll.Formula, 3) = "=IF" Then
cll = cll.Value
End If
Next

Why is cll.Value=cll not the same as cll=cll.Value

Thanks

Juan Pablo González

= positioning
 
The problem arises when you don't declare cll, or in other words, when cll
is declared as a variant.

In the second case, VBA evaluates first

"cll.Value"

where cll is a Range object, and then assigns whatever its value is (a
Double, or a String for example) to a "new" cll variable, not the original
Range object.

In the first case, VBA evaluates first

cll

and since at this point, it is still a Range object, VBA uses the default
property, which is .Value, so that line is equivalent to

cll.Value = cll.Value

however, since you're explicitly calling a property of the object, VBA
doesn't create the "new" variable, but instead uses the same reference to
the Range, therefor changing the formulas into the values, as you would
expect.

Bottom line, always declare your variables, and put "Option Explicit" at the
top of your modules.

--
Regards

Juan Pablo González

"snax500" wrote in message
om...
Why does it matter where you put the "=" in a macro (see line 3). I am

range
valuing cells that contain the "If" formula. In the following code:

For Each cll In Range("v10:v44")
If Left(cll.Formula, 3) = "=IF" Then
cll.Value = cll
End If
Next

why does this range value my cells but this next code does not:

For Each cll In Range("v10:v44")
If Left(cll.Formula, 3) = "=IF" Then
cll = cll.Value
End If
Next

Why is cll.Value=cll not the same as cll=cll.Value

Thanks





All times are GMT +1. The time now is 02:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com