简体   繁体   English

在使用VBA对excel文件执行所有操作后,如何取回原始文件?

[英]How do I get back my original file after performing all the Operations on the excel file using VBA?

Can Anyone tell me how do I undo all my changes to my workbook? 任何人都可以告诉我如何撤消对工作簿的所有更改? I have file excel1.xlsx and I have did sorting and many operations on the excel.xlsx using vba. 我有文件excel1.xlsx,我已经使用vba对excel.xlsx进行了排序和许多操作。 But at the end I want the excel1.xlsx to be the same which was at the start. 但最后我希望excel1.xlsx与开头的相同。 How do i Undo all my changes using vba? 如何使用vba撤消所有更改?

 activeworkbook.saved = True

I have found that it retains back all the contents as at the begginning but its not working.So is there any command where i can get back my original file after performing operations over it. 我发现它保留了所有内容,因为它没有工作。所以有任何命令,我可以在对它执行操作后取回原始文件。 Well yes 嗯,是

              wb1.Sheets(1).Activate
              ActiveWorkbook.Close savechanges:=False

It works but I dont want my workbooks to be closed it should be still opened. 它工作但我不希望我的工作簿被关闭它应该仍然打开。 How do I make it? 我怎么做到的? Thanks in advance. 提前致谢。

In order to undo a sub routine, you can either choose not to save the file and just close it, or you have to write a special sub routine to save the state of the file, then restore the state (custom undo). 要撤消子例程,您可以选择不保存文件并只关闭它,或者您必须编写一个特殊的子例程来保存文件的状态,然后恢复状态(自定义撤消)。 This is one of the pains with sub routines is that they cannot be undone through normal undo. 这是子例程的一个难点,就是它们无法通过正常的撤消来解除。 Most people, me including, will reccomend you work off a backup. 包括我在内的大多数人都会建议您使用备份。

When making your custome undo routine, the big question is what do you need to save the state for? 在进行自定义撤销程序时,最重要的问题是您需要将状态保存为什么? Saving all information about the file would be unnessesarily heavy, so it's good to know what you want to save. 保存有关该文件的所有信息将非常繁重,因此最好知道要保存的内容。

Update : This is a dirty way to backup the sheet if you only have 1 sheet of data. 更新 :如果您只有一张数据,这是一种备份工作表的脏方法。 This is more of a proof of concept of one way to create a backup and not finalized perfect code. 这更像是一种创建备份而非最终完美代码的方法的概念证明。 It just creates a backup copy of the currentsheet and when you'restore' you are simply deleting the original sheet and renaming the backup to what it used to be called. 它只是创建了电流表的备份副本,当你恢复时,你只需要删除原始工作表并将备份重命名为以前调用的内容。 :p :p

How to test: Put some data and value in your original sheet then run the Test() sub-routine! 如何测试:在原始工作表中放入一些数据和值然后运行Test()子程序!

Public backupSheet As Worksheet
Public originalSheet As Worksheet
Public originalSheetName As String

Sub CreateBackup()
    Set originalSheet = Application.ActiveSheet
    originalSheetName = originalSheet.Name
    originalSheet.Copy After:=Sheets(Application.Worksheets.Count)
    Set backupSheet = Application.ActiveSheet
    backupSheet.Name = "backup"
    originalSheet.Activate
End Sub

Sub RestoreBackup()
    Application.DisplayAlerts = False
    originalSheet.Delete
    backupSheet.Name = originalSheetName
    Application.DisplayAlerts = True
End Sub

Sub ZerosFromHell()
    Range("A1:Z100").Select
    Cells.Value = 0
End Sub

Sub Test()
    Call CreateBackup
    Call ZerosFromHell
    MsgBox "look at all those darn 0s!"
    Call RestoreBackup
End Sub

Short answer: you can't. 简短的回答:你做不到。 Sorry. 抱歉。

Changes you make to your sheet using VBA cannot be undone at the click of a button or with a single, standard VBA statement. 只需单击按钮或使用单个标准VBA语句,就无法撤消使用VBA对工作表所做的更改。

The right thing to do would seem to be: do your VBA-driven work on a copy of the sheet, and delete/don't save this copy if you don't want to keep the changes (and reopen the original if you need to do so). 正确的做法似乎是:在工作表副本上执行VBA驱动的工作,如果您不想保留更改,则删除/不保存此副本(如果需要,请重新打开原件)这样做)。 But from your question, it sounds like you don't want to do that. 但是从你的问题来看,听起来你不想这样做。

Your only alternative is then to write your own VBA procedure that backtracks all the changes you've done. 然后,您唯一的选择是编写自己的VBA程序,以回溯您所做的所有更改。 Depending on what operations you performed, reversing them could be a ridiculously complicated thing to do, compared to just closing without saving and re-opening. 根据您执行的操作,与仅关闭而不保存和重新打开相比,将其反转可能是一件非常复杂的事情。 But if you insist, by all means, knock yourself out! 但是,如果你坚持,无论如何,要把自己打倒!

  1. Save a copy of the original workbook prior to running your macro. 在运行宏之前保存原始工作簿的副本。 using the .SaveAs method at the beggining of the sub routine. 在子例程的开始使用.SaveAs方法。
  2. Run the VBA macro routine in the original workbook. 在原始工作簿中运行VBA宏例程。
  3. Now have a second macro "Undo VBA changes" that opens the workbook copy from step (1) , closes the workbook that ran the macro in Step (2) and calls the .SaveAs method again overwriting the existing workbook from step (2). 现在有第二个宏“撤消VBA更改”,它从步骤(1)打开工作簿副本,关闭在步骤(2)中运行宏的工作簿,并再次调用.SaveAs方法,从步骤(2)覆盖现有工作簿。

Note: In order to get this UndoMacro to work you will need to put it in an Addin or a seperate workbook (an addin is cleaner). 注意:为了使这个UndoMacro工作,你需要将它放在一个Addin或一个单独的工作簿中(一个插件更干净)。 This will allow you to run the .SaveAs method and overwrite teh original workbook from Step (2) which will at this point have been closed to prevent an VBA runtime error message occuring. 这将允许您运行.SaveAs方法并覆盖步骤(2)中的原始工作簿,此时将关闭此工作簿以防止发生VBA运行时错误消息。

If all of your data is cleanly organized, this works pretty well. 如果您的所有数据都是干净整洁的,那么效果非常好。 Much like the OP, I needed to go back to the original state of an Excel file, and didn't want to have to re-load the original (it takes about 25 seconds to load, due to aged infrastructure, and slow PCs). 就像OP一样,我需要回到Excel文件的原始状态,并且不想重新加载原始文件(由于老化的基础设施和慢速PC,加载大约需要25秒) 。

What I did was to copy all of the data into a variant array, do all of my processing to the workbook, then write back the variant array to the Excel file (data is on a worksheet called "Data", and starts at Row 2, and uses columns A through Z). 我所做的是将所有数据复制到变量数组中,对工作簿执行所有处理,然后将变量数组写回Excel文件(数据位于名为“Data”的工作表上,并从第2行开始,并使用A到Z列。 While this uses a bit of RAM, the reading/writing is nearly instantaneous. 虽然这使用了一点RAM,但读/写几乎是瞬时的。 Relevant code: 相关代码:

Dim varDataArray As Variant, wb As Workbook, ws As Worksheet, lngEndRow as Long

Set wb = ThisWorkbook
Set ws = ThisWorkbook.Worksheets("Data")

With ws
 ' This simply finds the last row with data
 lngEndRow = .Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
 ' This reads all cell data from A2 to Z###, where ### is the last row
 varDataArray = .Range("A2:Z" & lngNumberOfRows).Value

 ... do things ...

 ' This writes all the data back to Excel
 ' 26 is the numeric column equivalent to "Z"
 .Range("A2").Resize(lngEndRow, 26) = varDataArray

End With

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Excel VBA,如何在第二个工作簿中的“ thisworkbook.close”事件之后使我的原始代码继续执行? - Using Excel VBA, how do I keep my original code executing after a 'thisworkbook.close' event in a 2nd workbook? Excel VBA另存为原始文件格式 - Excel vba save as using original file format 如何使用VBA Excel 2007确定文件是否存在? - How Do I Determine If File Exists Using VBA Excel 2007? 如何使用vba从excel文件中删除逗号? - How do i remove commas from an excel file using vba? 当我创建Excel文件的副本时,它会创建指向原始文件的链接。 如何停止呢? - When I create a copy of my excel file it creates a link to the original. How do I stop this? 为什么在 VBA 中使用 VLookup 引用另一个 Excel 文件时会出现 #N/A? - Why do I get #N/A's when using VLookup in VBA to reference another Excel file? 如何使用 VBA 从 EXCEL 表中将所有项目排序到文本文件中? - How to get all items ordered into a text file from an EXCEL sheet using VBA? 如何在Excel中使用VBA获得3列的所有不同唯一组合? - How do I get all the different unique combinations of 3 columns using VBA in Excel? 我如何在Excel中使用VBA获得2列的所有不同唯一组合并求和第三个 - How do I get all the different unique combinations of 2 columns using VBA in Excel and sum the third 在 Excel 中使用 VBA,如何获取服务器上所有 OLAP 多维数据集的名称? - Using VBA in Excel, how do I get the names of all OLAP Cubes on a server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM