如何在执行 Excel 工作簿包含的 VBA 宏时重新打开该工作簿?
我已经在我的 Excel 文件上做了各种各样的事情,最后我需要返回到最后保存的版本。所以我关闭并重新打开我的 Excel 文件而不保存,这有效。在关闭工作簿之前,我已经复制了一系列单元格(通过使用一些排序和过滤等内容创建),现在我需要在重新打开它时将它们粘贴到文件末尾。
但是,当我们重新打开工作簿时,之前运行的宏不会在停止处继续。我想在宏再次重新打开时运行它停止的位置。这可能吗?
我的想法是在工作簿打开时放置此代码:
sub Auto_open()
sheet1.activate
' and select the last row of the sheet1
activesheet.paste
end sub
因为当工作簿重新打开时,它会自动执行此子例程并复制单元格内容。但缺点是我们不能说它的单元格总是被复制不足,因为如果用户将一些数据复制到剪贴板,然后由于 Auto_open 子文件打开该文件,它会将该数据复制到工作表的末尾,但这不是什么我想。
谁能提出一个好主意来解决这个问题?
I have done all sorts of things on my excel file and at the end I need to get back to the last saved version. So I close and reopen my excel file without saving, which works. Before closing the workbook I have copied a range of cells (created by using some sorting and filtering and stuff etc.) and now I need to paste them at the end of file when I reopen it.
But when we re-open a workbook, the previously running macro does not continue where it stopped. I want to run the macro where it stopped when it reopens again. Is that possible?
Well my idea is to put this code when the workbook opens:
sub Auto_open()
sheet1.activate
' and select the last row of the sheet1
activesheet.paste
end sub
because when the workbook reopens, it executes this subroutine automatically and copies the cells contents. But the drawback is we can't say its always cells will be under copied because if the user copies some data to the clipboard and then opens this file due to the Auto_open sub it copies that data to the end of sheet but that is not what I want.
Can anyone suggest a good idea to get over this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不暂时用不同的文件名
.SaveAs
修改后的工作簿呢?然后,您可以保持其打开状态,重新打开原始工作簿,并在两个工作簿都打开的情况下进行复制。最后,如果您愿意,可以删除修改后的工作簿。使用
_Open
事件只会导致灾难发生。此外,
.Copy
是一种非常容易出错的方法,因为它使用剪贴板,而其他应用程序也可以访问剪贴板,但两端的结果都不可预测。我从不单独使用.Copy
,但有时使用.Copy Destination:=...
这是安全的。但大多数情况下,我会做这样的事情:Why don't you just temporarily
.SaveAs
your modified workbook under a different filename? You can then keep it open, re-open the original workbook, and do the copying with both workbooks open. Finally, delete the modified workbook if you feel so inclined.This business with using the
_Open
event is just asking for disasters to happen.Also,
.Copy
is a terribly error-prone method, because it uses the clipboard, which other applications can access as well, with unpredictable results at both ends. I never use.Copy
alone, but sometimes use.Copy Destination:=...
which is safe. But mostly, I do something like this: