屏幕更新与选择问题-VBA Excel

时间:2018-08-07 15:13:49

标签: excel vba

我正面临一个奇怪的情况。我在具有许多功能的工作表上有一个按钮,它是打开另一个文件的功能之一:

        If Not IsItOpen(ENDERECO2) Then
            Workbooks.Open Filename:=ENDERECO1
        End If

        'ENDERECO2 has the file's name
        'ENDERECO1 has the full path of the same file
        'IsItOpen is a private function as follows:
        'Private Function IsItOpen(Name As Variant) As Boolean
        '    On Error Resume Next
        '    IsItOpen = Not (Application.Workbooks(Name) Is Nothing)
        'End Function

打开另一个工作簿后,如果尚未打开它,我将焦点移到第一张工作表上,因为我想在背景上打开第二个工作簿。为此,我使用:

        'At the very beggining of the code
        Dim CEL As Range
        Set CEL = Selection 
        'And at the end of it all
        CEL.Select

所有描述的代码都能完美运行。 我一直遇到的问题:由于此按钮可同时运行许多事情,因此我想在开头添加“ Application.Screenupdating = False”,并在末尾添加“ ... = True”,因此它不会闪烁计算时过多。事实是,当我添加Screenupdating内容时,它仍会按需打开第二个工作簿,但不会将焦点移回主工作簿。相反,它停在最近打开的工作簿上,并停留在那里。 CEL.Select命令上的Screenupdating可能会受到什么干扰? 有任何想法吗? 干杯

1 个答案:

答案 0 :(得分:1)

感谢塔尔辛。猜猜当我们不知道为什么时,我们即兴大声笑。效果很好:

    If Not IsItOpen(ENDERECO2) Then 
       Application.ScreenUpdating = True 
       Workbooks.Open Filename:=ENDERECO1 
       Application.ScreenUpdating = False 
    End If 

足够好了。干杯!