代码之家  ›  专栏  ›  技术社区  ›  Forward Ed

如何存储选择框左上角的位置

  •  0
  • Forward Ed  · 技术社区  · 9 年前

    我正在尝试存储选定单元格的左上角。稍后,下面的代码选择了许多不同的区域,在执行它之后,我想使所选单元格成为最初所选单元格的左上角。

    之后我在线路上出错 With Selection :

    Private Sub Test()
    
        Dim InsertPoint As Range
    
        With Selection
            Set InsertPoint = Range(.Row, .Column)
            'insert a bunch of code working with this selection
        End With
        'insert a whole wack of code selecting various things
        InsertPoint.select
    End Sub
    

    有人能告诉我正确的方向/纠正我的编码吗?

    1 回复  |  直到 9 年前
        1
  •  2
  •   user4039065 user4039065    9 年前

    当你在 With ... End With statement ,一切都以句点(又名 . 句点 )将与 Selection With…end With块引用。

    With Selection
        Set InsertPoint = .Cells(1)
        debug.print InsertPoint.address(0, 0)
        'do lots of stuff here
    End With
    
    debug.print InsertPoint.address(0, 0)
    

    所以 Cells(1) Cells(1, 1) 仍将引用工作表的A1,但在该块内 .Cells(1) 指“选择”左上角的单元格。