Roland
Roland

Reputation: 7853

Range.Cells property syntax

I'm confused by the Range.Cells property. According to MSN documentation:

Range.Cells Property (Excel)

The syntax is supposed to be expression .Cells

Yet at the example further down the code is not following this syntax but seems to use Cells as a function:

Worksheets("Sheet1").Activate 
Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

Upvotes: 2

Views: 561

Answers (1)

A.S.H
A.S.H

Reputation: 29332

When no dot . precedes a Cells or Range method, this is called an unqualified range reference; Excel's VBA wraps it automatically to the sheet that is currently active.

Cells(1, 1) <==> Activesheet.Cells(1, 1)
Range("A1") <==> Activesheet.Range("A1")

However, this is considered bad practice and it should be avoided because it leads to random issues and bugs; the code's behavior depends on what sheet the user currently has on top in Excel's GUI. Experienced developers always avoid it as much as possible and use qualified ranges; ie

Worksheets("someSheetName").Cells(1, 1).

Upvotes: 4

Related Questions