2015-12-29 62 views
0

我已經查看過,並且找不到具體的答案。下面的代碼提示用戶是否打開特定的文件。如果用戶點擊否,則子結束。如果他們點擊是,則子繼續。我已經測試了這個文件打開,所有的作品很好。但後來我忘了打開該文件,並根據提示是點擊並收到以下錯誤:檢查文件是否打開以防止出現錯誤

運行時錯誤「9」:

下標越界

對於此行的代碼:

隨着工作簿(「旋轉 - 主日 - 12月2015.xlsm」)。表(「旋轉」)

我明白爲什麼我得到的錯誤,但我該如何檢查,如果「是」回答用戶是真實的,以防止這個錯誤?

下面是完整的代碼:

Sub Extract_Sort_1512_December() 
' 
' 
    Dim ANS As String 
    ANS = MsgBox("Is the December 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 
    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 <> "12" 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) = "12" 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 - December 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 

我在這段代碼通過許多錯誤的工作在過去的兩天,我有點炒,所以任何幫助表示讚賞。

這裏是我的更新IF語句來檢查,以進行所需的工作簿的狀態:

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 
     ElseIf IsWBOpen("Swivel - Master - November 2015") Then 
    End If 
+0

嗯,你如果上面的測試應該工作。爲什麼把它留給用戶?通過代碼打開它。 – findwindow

+0

如果文件實際打開,所有這些都可以工作。當文件未打開並且用戶單擊是時出現錯誤。然後,當宏繼續並嘗試將數據複製到文件時,即出現錯誤時。我也已經完成了關於如何檢出並從SharePoint打開文件的研究,但是我無法讓它爲我工作。我認爲這與內部安全有關,但尚未證實。所以這就是爲什麼我要去MsgBox路線。 –

+2

啊分享點。在application.workbooks |中爲每個bk運行一個循環如果bk.name = Swivel - Master - December 2015.xlsm then flag =「open」else flag =「not open」'然後稍後對'flag'進行測試,如果flag = not open,則執行'exit sub'。說得通?編輯:'bk.name'可能會給整個路徑,因此檢查相應。 – findwindow

回答

1

使用此功能檢查,如果需要的工作簿打開:

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 

它看起來通過(已打開)工作簿的列表,並將該名稱與它的參數進行比較。擴展名被剝離,沒有路徑前置,比較是不區分大小寫的。
用法:
If IsWbOpen("Swivel - Master - December 2015") then '... proceed Else Exit Sub End If

+0

所以我很清楚理解(對VBA來說還是相當新的東西,並且由於SO而得到快速學習),我沒有在函數中命名工作簿,只是在If語句中?如果那是真的,那麼我可以在工作簿名稱略有不同的其他類似的子工具中使用If語句(如'Swivel - Master - November 2015')?所以這裏是一個愚蠢的問題,函數是否需要在它自己的模塊中? –

+1

你的第一個假設是100%正確的,通過使用該函數的參數,你可以普遍使用它。其次,功能需要在一個模塊中,但不一定在它自己的模塊中。如果您將其放入PERSONL.XLS中的模塊中,則可以從任何工作簿中使用它。 – user1016274

+0

小故障(我相信它是用戶錯誤(用戶=我))。我編輯了上面的原始問題,以顯示我用您提供的函數使用的IF語句。我沒有改變函數聲明。但是,當我運行代碼時,如果工作簿未打開並且用戶單擊「是」,我會得到以下錯誤:運行時錯誤「5」:無效的過程調用或參數。調試器指向函數中的這一行:name = LCase(Left(wb.name,InStrRev(wb.name,「。」) - 1))'strip extension。我究竟做錯了什麼? –