(VBA) when to use .cell (1,1) or .cell (1,1).

In my macros, when I refer to the value found in the cell, I inconsistently used either .cell (r, c) or .cell (r, c) .value.

dim c as variant 
c = sheet1.cell(1,1) 'or sheet1.cell(1,1).value

Are both links correct or is there a preferred way?

+4
source share
2 answers

.Value- The default property of the range object. Therefore, when you assign something like this:

myVar = myRange

it is equivalent myVar = myRange.Valuebecause you are assigning a variable, not an object.

However, if you use Set, for example:

Set myObj = myRange

. Set VBA, . Set VBA , .Value, .

.Value , :

1- , ,

2- VB.net Set ; . idiom VB.net. .Value VBA, VB.net.

+6

.Value2 ( vables ). .Value2, , Range, .

Dim r as Range
Set r = Range("A1")

Dim x as Double, i as Integer
x = r.Offset(1,0).Value2

Dim vals() as Variant
vals = r.Offset(1,0).Resize(10,1).Value2

For i=1 to 10
    vals(i,1) = CDbl(i)/10
Next i

r.Offset(1,0).Resize(10,1).Value2 = vals

, Cell(), . .Offset() .Resize() , . , Range("A1"), - Range("my_table") "my_table", , .


[]

Dim x as Double
x = [A2]
// This is the same as x = Range("A2").Value2
0

Source: https://habr.com/ru/post/1672693/


All Articles