检查文件是否打开以防止错误 - Pt。 2

时间:2015-12-30 19:33:10

标签: excel-vba excel-2007 vba excel

参考这篇文章: Checking if File is open to prevent error 我已经更新了代码,但现在我收到了:

运行时错误9: 下标超出范围

并且调试器突出显示该行代码(完整代码如下,以及IsWBOpen函数):

With Workbooks("Swivel - Master - November 2015.xlsm").Sheets("Swivel")

我唯一能想到的是.Sheets(“Swivel”)是罪魁祸首,但我不确定这一点。

这是我想要完成的事情:

如果用户单击“否”,则子节点以MsgBox消息结束,表明此过程将终止。 如果用户单击“是”并且工作簿打开,则用户会收到相同的消息,就好像他们点击了子结尾一样。 如果用户单击“是”并且工作簿已打开,则子继续。

这是功能:

Function IsWBOpen(WorkbookName As String) As Boolean
' check if WorkbookName is already opened; WorkbookName is without path or extension!
' comparison is case insensitive
' 2015-12-30

    Dim wb As Variant
    Dim name As String, searchfor As String
    Dim pos As Integer

    searchfor = LCase(WorkbookName)
    For Each wb In Workbooks
        pos = InStrRev(wb.name, ".")
        If pos = 0 Then                           ' new wb, no extension
            name = LCase(wb.name)
        Else
            name = LCase(Left(wb.name, pos - 1))  ' strip extension
        End If
        If name = searchfor Then
            IsWBOpen = True
            Exit Function
        End If
    Next wb
    IsWBOpen = False
End Function

这是主要的子目录:

Sub Extract_Sort_1511_November()
'
'
Dim ANS As String

    ANS = MsgBox("Is the November 2015 Swivel Master File checked out of SharePoint and currently open on this desktop?", vbYesNo + vbQuestion + vbDefaultButton1, "Master File Open")
    If ANS = vbNo Then
        MsgBox "This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
        Exit Sub
    Else
        If ANS = vbYes Then
            If IsWBOpen("Swivel - Master - November 2015.xlsm") Then
            End If
            Else
                MsgBox "This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
                Exit Sub
        End If
    End If

Application.ScreenUpdating = False

    ' This line renames the worksheet to "Extract"
    ' ActiveSheet.name = "Extract"

    ' This line autofits the columns C, D, O, and P
    Range("C:C,D:D,O:O,P:P").Columns.AutoFit

    ' This unhides any hidden rows
    Cells.EntireRow.Hidden = False

Dim LR As Long

    For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Range("B" & LR).Value <> "11" Then
            Rows(LR).EntireRow.Delete
        End If
    Next LR

With ActiveWorkbook.Worksheets("Extract").Sort
    With .SortFields
        .Clear
        .Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add Key:=Range("D2:D2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add Key:=Range("O2:O2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add Key:=Range("J2:J2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add Key:=Range("K2:K2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Add Key:=Range("L2:L2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
    .SetRange Range("A2:Z2000")
    .Apply
End With
Cells.WrapText = False
Sheets("Extract").Range("A2").Select

    Dim LastRow As Integer, i As Integer, erow As Integer

    LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Cells(i, 2) = "11" Then

            ' As opposed to selecting the cells, this will copy them directly
            Range(Cells(i, 1), Cells(i, 26)).Copy

            ' As opposed to "Activating" the workbook, and selecting the sheet, this will paste the cells directly
            With Workbooks("Swivel - Master - November 2015.xlsm").Sheets("Swivel")
                erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                .Cells(erow, 1).PasteSpecial xlPasteAll
            End With
            Application.CutCopyMode = False
        End If
    Next i

Application.ScreenUpdating = True
End Sub

findwindow和user1016274在获取代码方面非常有帮助。欢迎对此错误提供所有帮助。

1 个答案:

答案 0 :(得分:1)

改变这个:

Dim ANS As String

    ANS = MsgBox("Is the November 2015 Swivel Master File checked out of SharePoint and currently open on this desktop?", vbYesNo + vbQuestion + vbDefaultButton1, "Master File Open")
    If ANS = vbNo Then
        MsgBox "This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
        Exit Sub
    Else
        If ANS = vbYes Then
            If IsWBOpen("Swivel - Master - November 2015.xlsm") Then
            End If
            Else
                MsgBox "This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
                Exit Sub
        End If
    End If

到:

Dim ANS As Long

ANS = MsgBox("Is the November 2015 Swivel Master File checked out of SharePoint and currently open on this desktop?", vbYesNo + vbQuestion + vbDefaultButton1, "Master File Open")
If ANS = vbNo Or IsWBOpen("Swivel - Master - November 2015") = False Then
    MsgBox "This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
    Exit Sub                   
End If