第二次保存为CSV文件时Excel崩溃

时间:2017-04-12 14:07:21

标签: excel vba csv crash save

当我第一次运行代码时,它会替换现有文档并保存CSV文件没有问题。但是当我第二次运行代码时,Excel会在保存CSV时崩溃。

我有一个解决方法,在第一次运行代码重新打开Excel之后,但需要花费太多时间,任何想法?

Sub copiar_ayuda1()
  Application.DisplayAlerts = False
  With Sheets("input11")
    .Select
    .Move
  End With
  PathName = "" & ThisWorkbook.Path & "os.csv"

  ActiveWorkbook.SaveAs Filename:=PathName, FileFormat:=xlCSV, CreateBackup:=False

  Application.CutCopyMode = False
  ActiveWorkbook.Saved = True
  ActiveWorkbook.Close

End Sub

1 个答案:

答案 0 :(得分:0)

如果您尝试将Sheets("input11")保存为" CSV"文件,您需要做的就是:

Option Explicit

Sub copiar_ayuda1()

Dim PathName As String
Dim Sht As Worksheet

Application.DisplayAlerts = False

PathName = ThisWorkbook.Path & "\os.csv"

Set Sht = ThisWorkbook.Worksheets("input11")
With Sht
    .SaveAs Filename:=PathName, _
    FileFormat:=xlCSV, CreateBackup:=False
End With

Application.CutCopyMode = False

End Sub