Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:编译后出错。复制_Vba_Excel_Compilation_Copy - Fatal编程技术网

VBA:编译后出错。复制

VBA:编译后出错。复制,vba,excel,compilation,copy,Vba,Excel,Compilation,Copy,如果您有一个带有子文件夹的大型工作簿,则会将特定工作表复制到新工作簿,然后将此工作表另存为FileFormat51(xlsx,不带宏),以消除包含的代码: Public Sub savefile() Dim WB As Workbook, WBtemp As Workbook Dim path As String, antw As String, ext As String Dim filetobesaved path = ThisWorkbook.path With Application.

如果您有一个带有子文件夹的大型工作簿,则会将特定工作表复制到新工作簿,然后将此工作表另存为FileFormat51(xlsx,不带宏),以消除包含的代码:

Public Sub savefile()
Dim WB As Workbook, WBtemp As Workbook
Dim path As String, antw As String, ext As String
Dim filetobesaved

path = ThisWorkbook.path
With Application.FileDialog(msoFileDialogSaveAs)
    .InitialFileName = path & "\" & "standard name"
    Application.ScreenUpdating = True
    antw = .Show
    Application.ScreenUpdating = False
    If antw = -1 Then
        filetobesaved = .SelectedItems(1)
        ext = Right(filetobesaved, Len(filetobesaved) - InStrRev(filetobesaved, ".")) 'InStrRev() finds the first dot from the right and gives its position to Right() to write the file extension into ext
        If ext <> "xlsx" Then
            MsgBox "You chose ." & ext & " as filename extension. As this might cause problems during the current procedure I will change it to .xlsx."
            filetobesaved = Left(filetobesaved, InStrRev(filetobesaved, ".")) & "xlsx"
        End If    
    Else
        Exit Sub
    End If
End With

Set WB = ActiveWorkbook
TKontur.Copy
Set WBtemp = ActiveWorkbook

WBtemp.SaveAs Filename:=filetobesaved, FileFormat:=51  '51 = .xlsx     without macros
WBtemp.Close

End Sub
通常有助于在一段时间内忽略该错误,但之后(通常在一年左右之后),即使使用这两行代码,它也会再次出现。编辑:此解决方案似乎有20%的几率使Excel崩溃,包括所有其他打开的工作簿

最后一件事:一旦错误发生一次 大多数情况下,它会出现在所有PC和笔记本电脑上,包括不同的更新状态、操作系统和Office版本。

I在将工作簿保存为解决方案之前禁用事件(
Application.EnableEvents=False
)。这对我有用

此外,我更喜欢使用Excel常量名称,以提高可读性,而不是Excel常量值。例如,下面是主要的格式,我使用xlsx的xlOpenXMLWorkbook而不是51。虽然这与你或我所犯的错误无关

51 = xlOpenXMLWorkbook (without macro's in 2007-2016, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2016, xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007-2016, xls)
以下是修改后的代码供参考:

Public Sub savefile()
Dim WB As Workbook, WBtemp As Workbook
Dim path As String, antw As String, ext As String
Dim filetobesaved

path = ThisWorkbook.path
With Application.FileDialog(msoFileDialogSaveAs)
    .InitialFileName = path & "\" & "standard name"
    Application.ScreenUpdating = True
    antw = .Show
    Application.ScreenUpdating = False
    If antw = -1 Then
        filetobesaved = .SelectedItems(1)
        ext = Right(filetobesaved, Len(filetobesaved) - InStrRev(filetobesaved, ".")) 'InStrRev() finds the first dot from the right and gives its position to Right() to write the file extension into ext
        If ext <> "xlsx" Then
            MsgBox "You chose ." & ext & " as filename extension. As this might cause problems during the current procedure I will change it to .xlsx."
            filetobesaved = Left(filetobesaved, InStrRev(filetobesaved, ".")) & "xlsx"
        End If    
    Else
        Exit Sub
    End If
End With

Set WB = ActiveWorkbook
TKontur.Copy
Set WBtemp = ActiveWorkbook

Application.EnableEvents = False
    WBtemp.SaveAs Filename:=filetobesaved, FileFormat:=xlOpenXMLWorkbook ' = xlsx
    WBtemp.Close
Application.EnableEvents = True    
End Sub
Public子存储文件()
将WB设置为工作簿,WBtemp设置为工作簿
变暗路径为字符串、antw为字符串、ext为字符串
要保存的文件
path=ThisWorkbook.path
使用Application.FileDialog(msoFileDialogSaveAs)
.InitialFileName=路径&“\”和“标准名称”
Application.ScreenUpdating=True
antw=.Show
Application.ScreenUpdating=False
如果antw=-1,则
filetobesave=.SelectedItems(1)
ext=Right(filetobesaved,Len(filetobesaved)-InStrRev(filetobesaved,“.”)instrev()查找右侧的第一个点,并将其位置指定给Right(),以将文件扩展名写入ext
如果是ext“xlsx”,则
MsgBox“您选择了.&ext&”作为文件扩展名。由于这可能会在当前过程中导致问题,我将其更改为.xlsx。”
filetobesave=Left(filetobesave,InStrRev(filetobesave,“.”)和“xlsx”
如果结束
其他的
出口接头
如果结束
以
设置WB=ActiveWorkbook
收到
设置WBtemp=ActiveWorkbook
Application.EnableEvents=False
WBtemp.SaveAs文件名:=filetobesave,文件格式:=xlOpenXMLWorkbook'=xlsx
温度关闭
Application.EnableEvents=True
端接头

您好,第一位:谢谢您的回答,第二位很抱歉回复太晚。错误发生在.Copy上,因此这是我需要为其禁用事件的行。在我的例子中,从另一个子系统调用整个子系统,该子系统禁用该子系统的事件,因此禁用事件没有帮助。我还尝试为.Copy启用事件,但也没有帮助。我和其他一些人谈过,他们告诉我,他们认为这个错误实际上是由一个杀毒软件引起的,它试图偷偷摸摸地使用一些BS。解决方法是使整个工作簿与防病毒软件使用的版本非常相似。PS:我通常使用数字,因为它们编译得更快。(在编译过程中,常量名称将根据其编号进行计算。)颜色与此相同,您可以输入RGB或其名称。当然,我还将添加一条注释,解释它们的含义。在本例中,无论哪种方式都需要注释,因为有些人不知道“xlOpenXMLWorkbook”排除了宏。我确实会在数字看起来有异国情调或会被多次使用的情况下使用这些名称。例如,如果我使用Msgbox return vbNo 3次,我将把它存储在一个整数中,并解释为什么它是一,但检查vbNo,而不是7。
Public Sub savefile()
Dim WB As Workbook, WBtemp As Workbook
Dim path As String, antw As String, ext As String
Dim filetobesaved

path = ThisWorkbook.path
With Application.FileDialog(msoFileDialogSaveAs)
    .InitialFileName = path & "\" & "standard name"
    Application.ScreenUpdating = True
    antw = .Show
    Application.ScreenUpdating = False
    If antw = -1 Then
        filetobesaved = .SelectedItems(1)
        ext = Right(filetobesaved, Len(filetobesaved) - InStrRev(filetobesaved, ".")) 'InStrRev() finds the first dot from the right and gives its position to Right() to write the file extension into ext
        If ext <> "xlsx" Then
            MsgBox "You chose ." & ext & " as filename extension. As this might cause problems during the current procedure I will change it to .xlsx."
            filetobesaved = Left(filetobesaved, InStrRev(filetobesaved, ".")) & "xlsx"
        End If    
    Else
        Exit Sub
    End If
End With

Set WB = ActiveWorkbook
TKontur.Copy
Set WBtemp = ActiveWorkbook

Application.EnableEvents = False
    WBtemp.SaveAs Filename:=filetobesaved, FileFormat:=xlOpenXMLWorkbook ' = xlsx
    WBtemp.Close
Application.EnableEvents = True    
End Sub