出于某种原因,下面的宏在完成后会崩溃我的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
谢谢!
答案 0 :(得分:0)
可能它崩溃了,因为你没有在循环中限定你的对象,如下所示。
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。