自保存

时间:2017-05-29 13:45:36

标签: excel vba excel-vba

我在网上发现一个旧脚本关闭文档而不保存更改,然后重新打开文档:

Sub RevertFile()
  wkname = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
  ActiveWorkbook.Close Savechanges:=False
  Workbooks.Open Filename:=wkname
End Sub

我想要这个,因为您无法“撤消”因运行宏而导致的更改。但是,它似乎在MS Office v1609中不起作用。首先,文件在关闭后不会重新打开。其次,当我希望它们不被保存时,修改 。如何重写此脚本以使其工作?感谢。

[编辑]

这是我正在使用的另一个子程序。

Sub FixPlatforms()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet
Dim platList As Variant
Dim x As Long

platList = Array _
( _
    "PS4", "PlayStation 4", _
    "PS3", "PlayStation 3", _
    "PS2", "PlayStation 2", _
    "PSV", "PlayStation Vita", _
    "PSP", "PlayStation Portable", _
    "WIN", "Microsoft Windows", _
    "SNES", "Super Nintendo Entertainment System" _
)


'Loop through each item in Array lists
  For x = 1 To UBound(platList) Step 2
    'Loop through each worksheet in ActiveWorkbook
      For Each sht In ActiveWorkbook.Worksheets
        sht.Cells.Replace What:=platList(x), Replacement:=platList(x - 1), _
          LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
      Next sht

  Next x

End Sub

它有什么问题吗?

1 个答案:

答案 0 :(得分:4)

您无需在任何情况下关闭工作簿。尝试打开已打开的工作簿会产生以下结果。

enter image description here

添加application.displayalerts = false应该足以避免这种确认。

Option Explicit

Sub RevertFile()
    Dim wkname As String
    wkname = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
    Application.DisplayAlerts = False
    Workbooks.Open Filename:=wkname
    Application.DisplayAlerts = True
End Sub