PBeezy
PBeezy

Reputation: 1272

How is it that the Cells() property accepts decimal values?

If I execute the following code

Cells(3.0004, 5.604).Interior.ColorIndex = 1

it will color the cell at Row 3, Column 6 (F3) black. It does so without complaining and appears to silently round the values. How and why?

Upvotes: 3

Views: 76

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71217

The Range.Cells property is parameterless.

Property Cells As Range / read-only / member of Excel.Range

This means Cells(foo, bar) is an implicit default member call which resolves to something like this (assuming the code is written in a standard module):

 ActiveSheet.Cells.[_Default](foo, bar)

hidden Range._Default property

If the default member is invoked without parameters, what you get is a member call to the range's Value.

If the default member is invoked with parameters, what you get is a member call to the range's Item property:

range.item property

The RowIndex and ColumnIndex parameters are Variant, so you can give it "D" for a column and it understands you want the 4th. Or you can give it any integer value and it'll work with that.

So why is it happy to work with a decimal value? Because it's forgiving, that's all: it makes the implicit narrowing type conversion from Double to Long, and merrily takes 5.604 and makes that 6.

Upvotes: 5

Related Questions