Home > Software design >  Why "Activecell is Selection" is false when there is only one cell selected?
Why "Activecell is Selection" is false when there is only one cell selected?

Time:11-29

The Activecell Is Selection statement should be TRUE when there is only one cell selected. Could someone tell me why it is FALSE?

CodePudding user response:

Both ActiveCell and Selection return a Range object which in this case represent the same cell, but they are not the same VBA object even though they share the same properties.

Sub Tester()

    Dim r1 As Range, r2 As Range
    
    Set r1 = Selection
    Set r2 = ActiveCell
    Debug.Print r1.Address, r2.Address, r1 Is r2   '> $D$11   $D$11   False
    
    'this is the same thing but assigned differently...
    Set r1 = [D11]
    Set r2 = [D11]
    Debug.Print r1.Address, r2.Address, r1 Is r2  '> $D$11   $D$11   False
    
    Set r2 = r1 'now they refer to the same [VBA] object
    Debug.Print r1.Address, r2.Address, r1 Is r2  '> $D$11   $D$11   True

End Sub

CodePudding user response:

I think we have to compare the full address: ActiveCell.Address(External:=True)=Selection.Address(External:=True)

  • Related