我对VBA很新,所以我在网上找到了迄今为止所提出的内容。我有一本工作簿,是其他几本工作簿的高潮......因此它与其他几本工作簿有许多链接。我本质上需要复制工作簿数据,而不需要在公司外部发送链接。我找到了这样做的代码,它运行正常。问题是我希望每次保存工作簿时都会发生这种情况。我也发现了代码,但是当我尝试将两者结合起来时,excel崩溃了。以下是创建工作簿副本的代码:
Sub nowe_poprawione()
Dim Output As Workbook
Dim Current As String
Dim FileName As String
Set Output = ThisWorkbook
Current = ThisWorkbook.FullName
Application.DisplayAlerts = False
Dim SH As Worksheet
For Each SH In Output.Worksheets
SH.UsedRange.Copy
SH.UsedRange.PasteSpecial xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Next
FileName = ThisWorkbook.Path & "\" & "worksheet2.xlsx"
Output.SaveAs FileName, XlFileFormat.xlOpenXMLWorkbook
Workbooks.Open Current
Output.Close
Application.DisplayAlerts = True
End Sub
这是每次保存时运行宏的代码:
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
End Sub
这就是我想出的两个结合导致excel崩溃的原因:
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sub nowe_poprawione()
Dim Output As Workbook
Dim Current As String
Dim FileName As String
Set Output = ThisWorkbook
Current = ThisWorkbook.FullName
Application.DisplayAlerts = False
Dim SH As Worksheet
For Each SH In Output.Worksheets
SH.UsedRange.Copy
SH.UsedRange.PasteSpecial xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Next
FileName = ThisWorkbook.Path & "\" & "worksheet2.xlsx"
Output.SaveAs FileName, XlFileFormat.xlOpenXMLWorkbook
Workbooks.Open Current
Output.Close
Application.DisplayAlerts = True
End Sub
非常感谢任何帮助!
答案 0 :(得分:1)
您不能像子程序一样将子程序放在子程序中:
Sub foo()
Sub bar()
End Sub
相反,你可以从另一个调用一个子程序:
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Run "nowe_poprawione"
End Sub