Excel 2013 VBA为单个工作簿打开多个窗口

时间:2015-06-07 20:42:29

标签: vba excel-vba excel

问题: 当我使用excel vba为单个工作簿打开多个excel窗口时,当我尝试打开名称管理器时,excel会冻结。当我在2007版本的Excel中使用相同的代码时,不会发生此行为。

我没有打开任何加载项,如果我为同一个工作簿手动打开多个窗口,则不会发生此问题。我还对2013版办公室进行了维修,以确保它没有损坏。

我认为我的代码有问题导致问题。我没有看到问题是什么,并希望有人在这里可以告诉我我的代码是否有问题。代码运行没有问题,Windows打开后问题发生,我尝试打开名称管理器。

我通过调试器运行代码(逐步执行它)并且没有任何错误地完成了。我还确保没有可能导致问题的外部链接引用。

环境:

  • Windows 7
  • Microsoft Office 365(2013版)

代码:

Option Explicit
Global glbWkBkName As String
'---------------------------------------------------------------------------------------
' Procedure : InitWindows
' Author    : Ron
' Date      : 6/7/2015
' Called By : Workbook_Open
' Purpose   : Sets up 3 windows upon entering the workbook. Provides a view of 3 sheets
'             used while entering data. The middle sheet is where data entry is performed,
'             the left sheet provides reference information on the data being entered,
'             and the right sheet provides summary information about the data entered.
'---------------------------------------------------------------------------------------
'
Sub InitWindows()
    Dim wnWin As Window

    On Error GoTo InitWindows_Error

    glbWkBkName = ThisWorkbook.Name

    Application.ScreenUpdating = False

    'Get rid of all open windows to start at 1.
    'Easier than determining which windows are open and processing them.

    Do Until Windows.Count = 1
        Windows(Windows.Count).Close
    Loop

    'Create 2 more for a total of 3 windows.
    ActiveWindow.NewWindow
    ActiveWindow.NewWindow

    For Each wnWin In Windows
        Select Case wnWin.WindowNumber
                'Left window: SkillTreeLayout
            Case Is = 1
                wnWin.Activate
                Sheets("SkillTreeLayout").Select
                With wnWin
                    .WindowState = xlNormal
                    .Top = 6
                    .Left = 6
                    .Width = 514
                    .Height = 627
                    .DisplayGridlines = False
                End With
            Case Is = 2
                'Middle window: DataEnry
                wnWin.Activate
                Sheets("DataEntry").Select
                With wnWin
                    .WindowState = xlNormal
                    .Top = 6
                    .Left = 530
                    .Width = 698
                    .Height = 627
                    .DisplayGridlines = False
                End With
            Case Is = 3
                'Right window: DataEntrySummary
                wnWin.Activate
                Sheets("DataEntrySummary").Select
                With wnWin
                    .WindowState = xlNormal
                    .Top = 6
                    .Left = 1230
                    .Width = 200
                    .Height = 627
                    .DisplayGridlines = False
                End With
        End Select
    Next wnWin

    Debug.Assert glbWkBkName <> ""
    Set wnWin = Windows(glbWkBkName & ":2")

    Windows(glbWkBkName & ":2").Activate

    'Debug.Print glbWkBkName & ":2"
    'ClrSkillTreeCharData
ExitProcedure:
    On Error Resume Next
    Set wnWin = Nothing
    Application.ScreenUpdating = True
    Exit Sub

InitWindows_Error:
    Call UnexpectedError(Err.Number, Err.Description, Err.Source, _
    Err.HelpFile, Erl, "InitWindows")
    Resume ExitProcedure
End Sub

1 个答案:

答案 0 :(得分:1)

我做了一些额外的测试,发现如果我将第三个case语句中的窗口宽度修改为225,则excel不再冻结。我向微软公开了一个案例,他们同意这是Excel 2013中的一个错误。他们目前正在研究这个问题。

在这种情况下,excel 2013无法处理200或更低的窗口宽度。