自保存后恢复更改的宏 [英] Macro to revert changes since save

查看:59
本文介绍了自保存后恢复更改的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线找到了一个旧脚本来关闭文档而不保存更改,然后重新打开文档:

I found an old script online to close the document without saving the changes, then re-open the document:

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

我想要这个,因为您不能撤消"运行宏引起的更改.但是,它似乎在MS Office v1609中不起作用.首先,文档在关闭后不会重新打开.其次,当我不希望修改时保存 .如何重写此脚本以使其正常工作?谢谢.

I want this since you can't "undo" changes caused by running a macro. However, it does not seem to work in MS Office v1609. Firstly, the document does not re-open after it is closed. Secondly, the modifications are saved when I want them not to be. How can I rewrite this script to get it to work? Thanks.

这是我正在使用的另一个子例程.

Here is the other sub-routine I am using.

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

这有什么问题吗?

推荐答案

无论如何您都不必关闭工作簿.尝试打开已经打开的工作簿会产生以下结果.

You shouldn't have to close the workbook in any event. Attempting to open a workbook that is already open produces the following.

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

Adding application.displayalerts = false should be sufficient to avoid that confirmation.

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

这篇关于自保存后恢复更改的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆