VBA:Workbooks.Save&关闭不保存

时间:2017-07-19 07:25:04

标签: excel vba

我有以下代码正在运行但是.close行关闭了WB而没有保存:

Option Explicit
Public Function updateStatus(fpath As String, fname As String, num As String)

Dim wk As String, yr As String
Dim owb As Workbook
Dim trow As Variant

With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .EnableEvents = False
End With

Set owb = Application.Workbooks.Open(fpath & fname)

trow = owb.Sheets(1).Range("Change" & num).Row
owb.Sheets(1).Cells(trow, 5).value = "Test"

With owb
    .Save
    .Close SaveChanges:=True 'This line doesn't seem to work
End With

With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Function

如果我删除该行,则WB保持打开状态,我会看到更改。如果我添加该行,并打开特定文件,我认为没有变化。

1 个答案:

答案 0 :(得分:0)

如评论中所述,代码看起来没问题,您可能在Excel上启用了一些数据保护,但不允许保存。尝试制作一个最小的例子

Option Explicit

Public Sub TestMe()

    Dim owb As Workbook

    Set owb = Application.Workbooks.Open("C:\Users\vityata\Desktop\Testing.xlsx")
    owb.Save
    owb.Close

End Sub

然后使用F8进行调试,一旦通过owb.Save,就会看到您收到的消息。

只是你知道:

.Save
.Close SaveChanges:=True

使用.Save行,SaveChanges:=True部分无效。反之亦然。