运行宏后如何还原Excel工作表的准确位置

时间:2019-02-18 18:25:56

标签: excel vba

使用Excel VBA,如何将工作表记忆和还原为一组精确的可见行和列?

我有一个excel宏,它可以查看当前单元格的值,计算大量内容并在整个工作簿中运行以进行更改,然后将光标还原到原始单元格。但是原始单元格最终出现在窗口的第一行,这很烦人。我希望工作表的视图保持不变,不仅要记住原始工作表和单元格,然后选择它们,还要记住确切的可见行和列集!怎么做?

这有效:

Dim StartingSheet As String
Dim StartingCell As String
StartingCell = ActiveCell.Address
StartingSheet = ActiveSheet.Name
' do a lot of stuff
Worksheets(StartingSheet).Select
Range(StartingCell).Select

但是在宏的末尾,可见行和列的集合会移动,这很糟糕。

我想运行宏,而不在屏幕上看到任何动静。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

Set memorizevisiblerowsandcolumns=ActiveWindow.VisibleRange
'do stuff
memorizevisiblerowsandcolumns.Select
ActiveWindow.Zoom = True

答案 1 :(得分:0)

谢谢大家。对于我的海豚,一个好的解决方案是:

    Dim StartingSheet As String
    Dim StartingCell As String
    Dim StartingScrollRow As Integer
    Dim StartingScrollColumn As Integer
    StartingCell = ActiveCell.Address
    StartingSheet = ActiveSheet.Name
    StartingScrollRow = ActiveWindow.ScrollRow ‘ the row of the top left cell that is showing
    StartingScrollColumn = ActiveWindow.ScrollColumn ‘ the column of the top left cell showing
   ‘ do a bunch of stuff 
    Worksheets(StartingSheet).Select
    ActiveWindow.ScrollRow = StartingScrollRow ‘ make this row the top-most row that’s showing
    ActiveWindow.ScrollColumn = StartingScrollColumn ‘ make this col the left-most col showing
    Range(StartingCell).Select ' select the cell that was originally selected