Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro to revert changes since save

Tags:

excel

vba

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

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.

[edit]

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

Is there something wrong with it?

like image 374
posfan12 Avatar asked Feb 18 '26 11:02

posfan12


1 Answers

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

enter image description here

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