Excel通过宏保存被困

时间:2014-12-08 16:45:14

标签: excel vba excel-vba excel-2010

我有一个用Excel VBA编写的非常广泛的程序,除非它试图保存时才能正常工作。有时,不一定每次,宏都会保存工作簿(在这种情况下,导出为CSV),但是卡在标有“正在保存”的窗口上,这是一条状态消息,声称它正在保存我的文件和一个不会保存的进度条进一步发展。它还有一个标有“取消”的按钮,当按下该按钮时,它将继续宏,因为文件已经保存。

这是我正在使用的子程序:

Sub exportTable()
Dim varIsOpen As Boolean
Dim varSaveLocation1 As String, varSaveLocation2 As String
varIsOpen = False

If ThisWorkbook.Sheets("ControlSheet").Range("D2").Value = "" Then
    varSaveLocation1 = ThisWorkbook.Path & "\CSVREVIEW\"
    varSaveLocation2 = varSaveLocation1 & Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now)
Else
    varSaveLocation1 = ThisWorkbook.Sheets("ControlSheet").Range("D2").Value
    If Right(varSaveLocation1, 1) <> "\" Then varSaveLocation1 = varSaveLocation1 & "\"
    varSaveLocation2 = varSaveLocation1 & Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now)
End If
    For counter = 1 To Workbooks.Count
        If Workbooks(counter).Name = "TableBook.xls" Then varIsOpen = True

        If varIsOpen = True Then Exit For
    Next

    If varIsOpen = False Then GoTo isClosed

Workbooks("TableBook").Activate

'On Error Resume Next
If Len(Dir(varSaveLocation1, vbDirectory)) = 0 Then
   MkDir varSaveLocation1
End If
If Len(Dir(varSaveLocation2, vbDirectory)) = 0 Then
    MkDir varSaveLocation2
End If
'On Error GoTo 0

Application.DisplayAlerts = False

ActiveWorkbook.Sheets("test").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\test", xlCSV

ActiveWorkbook.Sheets("part").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\part", xlCSV

ActiveWorkbook.Sheets("logFile").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\logFile", xlCSV

ActiveWorkbook.Sheets("deltaLimits").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\deltaLimits", xlCSV

ActiveWorkbook.Close

Application.DisplayAlerts = True

isClosed:
End Sub

值得注意的是,我目前正在保存到服务器,但是不能真正记住它是否在本地目录上测试时这样做了。

怎样才能让它不挂?这个问题和/或解决方案的原因是什么?

2 个答案:

答案 0 :(得分:0)

似乎问题实际上只是它保存到服务器位置。在本地保存可以完美地运行程序。

感谢@TonyDallimore提出的建议。

答案 1 :(得分:0)

我的表开始出现这种情况-.saveas周围的两个doevent为我解决了这个问题。我不知道为什么需要它,或者为什么它起作用,我在帖子(我现在找不到)上看到了它。