is Cells collection or class?

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I read under worksheet object, cells is defined as the following

Property Cells As Range

Ok what I understand from the above statement is that Cells is an object of type Range, in another word Cells=Range. But what I do not understand, what does "property" mean here?

Also what "s" in Cells? s gives me impression it is Collection. Thank you very much
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Cells is NOT a RANGE
Cells is NOT a COLLECTON
Cells is a PROPERTY which returns a RANGE OBJECT

It is confusing :confused: because we are used to seeing abbreviated notation being used like this..
Code:
Cells [I]
OR[/I] 
ActiveSheet.Cells 
[I]OR[/I] 
ActiveSheet.Range("A1:XFD1048576")
instead of..
Code:
ActiveSheet.Range("A1:XFD1048576").Cells
- Cells is a property of the specified range

Run VBA below which helps illustrate this
- the first message box returns 4 identical values
- the other message box returns adresses of the first and last cell of range B11:T55
- the last line activates the last cell in range B11:T55

Code:
Sub CellsProperty()
Dim s As String, r1 As Range, r2 As Range, r3 As Range, r4 As Range, r5 As Range
'all cells
    Set r1 = Cells
    Set r2 = ActiveSheet.Cells
    Set r3 = ActiveSheet.Range("A1:XFD1048576")
    Set r4 = ActiveSheet.Range("A1:XFD1048576").Cells

    s = r1.Address(0, 0) & vbCr & r2.Address(0, 0) & vbCr & r3.Address(0, 0) & vbCr & r4.Address(0, 0)
    MsgBox s, , "All Cells"
    
'first and last cell in range
    Set r5 = ActiveSheet.Range("B11:T55")
    MsgBox "First" & vbTab & r5.Cells(1, 1).Address(0, 0) & vbCr & "Last" & vbTab & r5.Cells(r5.Rows.Count, r5.Columns.Count).Address(0, 0), , "Range" & vbTab & r5.Address(0, 0)

'select final cell in range
    r5.Cells(r5.Rows.Count, r5.Columns.Count).Activate
End Sub
 
Last edited:
Upvote 0
And the last line
Code:
    r5.Cells(r5.Rows.Count, r5.Columns.Count).Activate
could be also be written as
Code:
    r5.Cells(r5.Cells.Rows.Count, r5.Cells.Columns.Count).Activate
 
Upvote 0
Thanks for your help. But Cells is property inside Worksheet? does that mean Cells is property of worksheet and range as well?

If I say
Code:
 workbooks(1).worksheets(1).cells(1,1).value="hello"

does the line above is like the line below.

Code:
 workbooks(1).worksheets(1).range("a1:xfd1048576").cells(1,1).value="hello"

If yes, does that mean if I do not type range("a1:xfd1048576"), excel going to imply that anyway so the first line basically is the second line even when I do not type range("a1:xfd1048576"). Thank you once again.

Sorry one more thing, why few people user

Code:
Sheets.range("??:??").cells(?,?)

compare to this

Code:
Sheets.cells(?,?)
 
Last edited:
Upvote 0
Cells is property inside Worksheet? does that mean Cells is property of worksheet and range as well?
- yes (although I tend to think of the worksheet Cells property as a property of the "RANGE OF ALL CELLS of the worksheet")

why few people use
Sheets.range("??:??").cells(?,?)
compare to this
Sheets.cells(?,?)

Those 2 would be identical only when Range("??:??") begins with cell A1

But it is very useful being able to refer to specific cells of a specified range like this
Code:
Sub CellsProperty()
    Const t = vbTab
    Const n = vbCr
    Dim rng As Range, a1$, a2$, a3$, b1$, b2$, b3$
    Set rng = ActiveSheet.[COLOR=#000080]Range("A10:Z20")[/COLOR]

'top left cell of the range
    a1 = rng.Cells(1, 1).Address(0, 0) & t
    'same as
    a2 = rng.Cells(1).Address(0, 0) & t
    'same as
    a3 = ActiveSheet.Range("A10:Z20").Cells(1, 1).Address(0, 0)
    
'bottom right cell of the range
    b1 = rng(rng.Rows.Count, rng.Columns.Count).Address(0, 0) & t
    'same as
    b2 = rng.Cells(rng.Rows.Count, rng.Columns.Count).Address(0, 0) & t
    'same as
    With ActiveSheet.Range("A10:Z20")
        b3 = .Cells(.Rows.Count, .Columns.Count).Address(0, 0)
    End With

    MsgBox a1 & a2 & a3 & n & b1 & b2 & b3, , rng.Address(0, 0)

End Sub

Do not get too hung up about this :)
- here are some interesting examples which all return CV100
Code:
Sub UnexpectedPerhaps()
    Const n = vbCr
    Dim c1$, c2$, c3$, c4$, c5$, c6$, c7$
    c1 = Range("A1:B1").Cells(100, 100).Address(0, 0) & n
    c2 = Range("A1").Cells(100, 100).Address(0, 0) & n
    c3 = ActiveSheet.Cells(100, 100).Address(0, 0) & n
    c4 = ActiveSheet.Range("A1:XFD1048576").Cells(100, 100).Address(0, 0) & n
    c5 = ActiveSheet.Range("A1:XFD1048576")(100, 100).Address(0, 0) & n
    c6 = [a1].Cells(100, 100).Address(0, 0) & n
    c7 = Cells(1).Cells(100, 100).Address(0, 0)
    MsgBox c1 & c2 & c3 & c4 & c5 & c6 & c7
End Sub


And in case you are wondering
Code:
Dim a1$
is the same as
Code:
Dim a1 As String

Run the above from Excel not from inside VBA window
 
Last edited:
Upvote 0
Thanks for your reply and help. Could you please tell me what address(0,0) is used for here, why it is needed here. Thank you very much.

c1 = Range("A1:B1").Cells(100, 100).Address(0, 0) & n
 
Upvote 0
.Address by itself returns an absolute range reference for the address, i.e. "$A$1:$B$1"
Adding the (0, 0) removes the absolute (dollar) signs, i.e. "A1:B1"

This is easy enough to see with a simply example.
Code:
Sub Test()


    Dim rng As Range
    Set rng = Range("A1:B1")
    
    MsgBox "Address without (0,0): " & vbCrLf & rng.Address
    MsgBox "Address with (0,0): " & vbCrLf & rng.Address(0, 0)
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top