When one should use Set [e.g for SpecialCells return value]?

后端 未结 4 1282
逝去的感伤
逝去的感伤 2020-11-27 23:39

I am afraid I misunderstand the documentation of VBA for excel, I have this line which seems to be an error:

Range a = Selection.SpecialCells(xlCellTypeConst         


        
4条回答
  •  情歌与酒
    2020-11-28 00:35

    Do not know it this matters, but Value is not default property of Range. Default property of Range is _Default.

    enter image description here

    It is defined like this _Default([in, optional] VARIANT RowIndex, [in, optional] VARIANT ColumnIndex) and it represents the array of values of particular range.

    Sub test()
    
        Dim rng As Range
        Set rng = ActiveSheet.Range("A1:C3")
    
        ' all cells in range rng become value of 1
        rng.Value = 1
    
        ' all cells in range rng become now value of 2
        rng.[_Default] = 2
    
        ' first cell in range rng become value of 3
        rng.[_Default](1, 1) = 3
    
        ' nothing changes
        rng.Value()(1, 1) = 4
    
        Dim a1, a2
        ' however both Value and _Default return same array of variants
        a1 = rng.Value
        a2 = rng.[_Default]
    
    End Sub
    

提交回复
热议问题