簡體   English   中英

Workbook.close 導致 sub 退出

[英]Workbook.close causes sub to exit

我有一組版本化的 Excel 文檔,當有新版本可用時,我試圖自動更新這些文檔。 失敗的是.close方法不僅關閉其中一個工作簿,而且還退出子。

過程:從 Worksheet_Activate 調用 sub 並立即檢查是否需要升級。 如果需要,它會收集工作表的所有名稱(“計數”工作表除外,它是模板的副本),創建一個與舊工作表具有相同工作表的新工作簿,將數據復制到正確的工作表中,關閉舊工作簿,刪除舊工作簿,使用與舊工作簿相同的名稱保存新工作簿。

非常直接,它運行良好,直到它沒有。 我不知道為什么,但現在當wkbFrom.Close命令被執行時,它也會退出該過程。

我一直在四處尋找,我能找到的似乎解決我的問題的唯一答案是在關閉之前/之后進行一些延遲,以便 Excel 有時間完成而不與自身發生沖突。 所以我嘗試在關閉命令之前延遲 5 秒,但無濟於事。

Excel 沒有崩潰,它仍然正常運行。 我檢查了事件查看器,Excel 沒有拋出任何錯誤。 sub 只是關閉工作簿,然后退出 sub。

這是子的完整代碼。

    Sub UpgradeHWWorkbook(Optional HWSheetVersion As Double)

'--------------------------------
'This sub upgrades a hardware tracking
'workbook to the newest version based on
'version in the variable HWSheetVersion
'--------------------------------

'Before anything else, Check to see if upgrade is needed.
'If sheet version is equal or larger than the plugin version
'OR the name of the sheet is wrong, exit without upgrading
'---------------------------------------------------------------------
    If HWSheetVersion >= HWPlugInVersion Or _
        Not ActiveSheet.CodeName Like "BaseHWSheet_*" Then
            Exit Sub
    End If
'---------------------------------------------------------------------

'VAR declarations----------------
    Dim wkbFrom As Workbook         'Holds the original workbook
    Dim wkbTo As Workbook           'Holds the new workbook
    Dim sWKB As Workbook            'Holds Workbook where Count sheet is kept
    Dim sWKS As Worksheet           'Holds Count sheet
    Dim wks As Worksheet            'Holds worksheets
    Dim wksNames() As String        'Holds the names of all the worksheets
    Dim wkbFromName As String       'Holds the name of the original workbook
    Dim wkbFromPath As String       'Holds the path of the original workbook
    Dim wkbToPath As String         'Holds the path where the new workbook will be saved
    Dim rng As String               'Holds the range of cells that will be copied
    Dim x As Byte                   'Holds counter
    Dim wksName As Variant          'Holds the name of the current worksheet
'--------------------------------

'Sub Settings--------------------
    Set wkbFrom = ActiveWorkbook                                    'Set the active workbook as the one that the data comes from
    wkbFromPath = wkbFrom.Path                                      'Grabs the path of the original workbook
    wkbFromName = wkbFrom.Name                                      'Grab the original workbook name
    wkbToPath = wkbFrom.FullName                                    'Grab the path path and name in another var so we don't have to do it by hand
    ReDim wksNames(0)                                               'Starts off the array that will hold the worksheet names
    x = 0                                                           'Flush the counter
    rng = "A2:D18"                                                  'The range of cells that will be copied and pasted
    Application.DisplayAlerts = False                               'Turn off annoying pop-ups
    Set sWKB = Workbooks("StockroomAddins.xlam")                    'Workbook with Count sheet to copy to new workbook
    Set sWKS = sWKB.Worksheets("Count")                             'Count sheet to copy to new workbook
'--------------------------------


'Get all of the worksheet names (except Count) in the workbook
'-----------------------------------------------------------------------
    For Each wks In wkbFrom.Worksheets                              'itenerate through the book
        If Not wks.Name = "Count" Then                              'If the worksheet isn't the "Count" sheet...
            wksNames(x) = wks.Name                                  'add the sheet name to the array wksName()
            x = 1 + UBound(wksNames)                                'Increase the array by 1
            ReDim Preserve wksNames(x)                              'Increase the size of the array by 1
        End If
    Next wks
'-----------------------------------------------------------------------


'Create new workbook & add Count sheet
'-----------------------------------------------------------------------
    Set wkbTo = Workbooks.Add                                       'Create the new workbook
    wkbTo.Activate                                                  'Make sure new book is active book
    sWKS.Copy Before:=Sheets("Sheet1")                              'Add the Count sheet to workbook
'-----------------------------------------------------------------------

'Iterate through the sheets in the original workbook, add sheets with the same name to the new book, copy data from the old sheet to the new sheet
'-----------------------------------------------------------------------
    For Each wksName In wksNames                                    'Loop through all of the worksheet names and...
        If Not wksName = "" Then                                    'If it isn't blank...
            Call NewHardwareTrackingSheet(wksName, wkbTo)           'Call the sub that creates a new tracking sheet
            wkbFrom.Worksheets(wksName).Range(rng).Copy             'Copy the data from the old sheet
            wkbTo.Worksheets(wksName).Range(rng).PasteSpecial _
                Paste:=xlPasteValues                                'Paste the data (Values only) into the new sheet
        End If
    Next wksName
    
    wkbTo.Worksheets("Sheet1").Delete                               'Delete the default "Sheet 1" that every new workbook has

    wkbFrom.Close Savechanges:=False                                'close the original workbook
'-----------------------------------------------------------------------

    
'Delete the old workbook and save the new one in the same place with the same name as the old one
'-----------------------------------------------------------------------
    Kill wkbToPath                                                  'Kill the original
    wkbTo.SaveAs Filename:=wkbToPath, FileFormat:=52                'Save the new as the original
    
    Application.DisplayAlerts = True                                'Turn annoying pop-ups back on
'-----------------------------------------------------------------------

'Clean up-------------------------------------
Set wkbFrom = Nothing: Set wkbTo = Nothing: Set sWKB = Nothing
Set wks = Nothing: Set sWKS = Nothing
'---------------------------------------------
    

End Sub

關於我搞砸的事情有什么想法嗎? 我想,因為它曾經工作過,現在沒有,我可能在某處搞砸了代碼,但我沒有看到它。

好的,我找到了我的答案,我對此感到有些愚蠢。 感謝那些問我一些問題的人,因為他們引發了有效的思考過程。

我用來測試代碼的電子表格似乎已經損壞了。 我在其他幾個文件上試過它,它工作正常。 難怪我找不到代碼問題:沒有。

去展示古老的格言“測量兩次,切割一次”。 我應該對多個文件進行測試,而不是假設我的單個測試文件是正確的。

非常感謝那些閱讀、思考和評論我的帖子的人。 值得贊賞。

編輯:或不......

今天早上來了,它又不工作了。 我的代碼中一定有一些東西會導致某些問題而不是其他問題。 TBH,我不知道它是什么。

這真的讓我把頭撞在牆上。

好的,所以我想我已經弄清楚了。

每次激活工作表時,我都會調用此 Sub 來檢查版本:

Public Sub Worksheet_Activate()
   Application.Run "StockroomBarcodeSheets.UpgradeHWWorkbook", HWSheetVersion
End Sub

為了進行其他一些測試,我在我的插件中設置了另一個子程序,該子程序使用虛假的 HWSheetVersion 調用 UpgradeHWWorkbook 子程序,以便我可以強制升級工作簿。 瞧,這個設置每次都能完美運行。

因此,當我從 Worksheet_Activate() 調用時,它會在.close命令上退出。 當我從加載項內的子調用它時,它工作得很好。

因為 UpgradeHWWorkbook 在插件中,我認為關閉調用工作簿的限制不會生效。 我錯了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM