Range, cells (and element) in non-serial ranges

I looked at the Holene question here and, trying to understand the MSDN documentation, I came up with the following problem. can anyone explain why the following lines give two different results?

Sub test()
    Debug.Print "With Cells(2)"
    Debug.Print Intersect(Range("B:B, C:C"), Rows(1)).Cells(2).Address 'prints $C$1
    Debug.Print Intersect(Range("B:B, D:D"), Rows(1)).Cells(2).Address 'prints $B$2
    Debug.Print Range("B:B, C:C").Cells(2).Address 'prints $B$2
    Debug.Print Range("B:B, D:D").Cells(2).Address 'prints $B$2
    Debug.Print "With Item(2)"
    Debug.Print Intersect(Range("B:B, C:C"), Rows(1)).Item(2).Address 'prints $C$1
    Debug.Print Intersect(Range("B:B, D:D"), Rows(1)).Item(2).Address 'prints $B$2
    Debug.Print Range("B:B, C:C").Item(2).Address 'prints $B$2
    Debug.Print Range("B:B, D:D").Item(2).Address 'prints $B$2
End Sub
+4
source share
3 answers

Many VBA range methods work fine in the adjacent range, but have special behavior when the range has several areas, i.e. they apply to the first area of ​​the range. Cells (i) are one of them, although if they span a range with a loop in the sort For Each cell in myRange, they will work correctly and span all cells, even if the span has multiple areas.

Intersect B1: C1, , , 2 C1. (i) .

Intersect : (B1, D1). (i) , B1.

ps: Range.Cells(), , , .

:

(B: B, C: C) - , - B, - C. (B: C), . , , . , . : .

+2

Cells, , , .

, , , , , .

, :

Range("A1:C1").Cells(3).address

$C $1. 4 , , $A $2.

: , . :

Range("A1").Cells(rows.Count).address

$A $1048576, :

Range("A1").Cells(rows.Count + 1).address

, $B $1, (.. A).

+2

Intersect(Range("B:B, C:C"), Rows(1)) $B$1:$C$1. Intersect(Range("B:B, D:D"), Rows(1)) , $B$1,$D$1.

Area. .

, Area, .

$B$1:$C$1 $B$1:$C$1, $C$1.
$B$1,$D$1 $B$1, ( !) $B$2.

+1

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


All Articles