簡體   English   中英

Microsoft Excel VBA宏崩潰Excel

[英]Microsoft Excel VBA Macro is crashing excel

由於某種原因,下面的宏在完成時使Excel崩潰。 該工作簿將保存,並將刪除我要求的工作表。 然后,它使Excel崩潰,並詢問我是否要重新啟動它。 有偶然的想法嗎? 這里的概念是,我們已將excel鏈接到數據多維數據集,並希望創建一個“值副本”,其中僅包含沒有公式鏈接到數據庫的值。

Sub CreateVC()
'Set file path & target file name. Make sure you update the department within the file
'name in cell H2. I.E. change "Reporting - Rampage VC.xlsm" to "Reporting - VAD VC.xlsm"

Dim WBFile As Range
Set WBFile = ActiveSheet.Range("H2")

'Save current workbook
ActiveWorkbook.Save

'Save your workbook as a new file with the name at the end of the filepath in cell H2

ActiveWorkbook.SaveAs (WBFile)

'Moves your view to the first tab, Cognos Parameters in this case. It then moves to the next
'tab, copies the entire page, and does a paste as values. The loop will continue until the
'worksheet index # equals the total number of worksheets (it hits the last worksheet).
Worksheets(1).Select
 Do While ActiveSheet.Index <> Worksheets.Count
 ActiveSheet.Next.Select
 Cells.Select
 Selection.Copy
 Selection.PasteSpecial Paste:=xlPasteValues
 ActiveSheet.Range("H6").Select
 Loop

'Delete the first tab, Cognos Parameters, as it is not used in the value copy. This is also
'a safeguard from creating another VC by deleting the button linked to the macro.

Worksheets(1).Delete

'Save the new VC file that has been created.

ActiveWorkbook.Save

End Sub

謝謝!

可能是崩潰的原因是您尚未在循環中限定對象,如下所示。

With ActiveSheet
    Do While .Index <> .Worksheets.Count
         .Next.Select
         .Cells.Select
         .Selection.Copy
         .Selection.PasteSpecial Paste:=xlPasteValues
         .Range("H6").Select
     Loop
End With

我還建議刪除SaveAs中WBFile周圍的括號,因為這也可能會引起問題。

ActiveWorkbook.SaveAs WBFile

您還應該嘗試不要使用Select和ActiveSheet。

暫無
暫無

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

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