我正在尝试打开多个excel文件(97,2003,2007,2010..等),编辑它们并保存它们。如果在开始时打开了Visual Basic编辑器(Alt+F11)并要求启用宏,则宏可以工作,但如果关闭了VB编辑器,它将打开该文件,并在保存第一个文件时停止。
我使用以下命令:
Set OpenWB = Workbooks.Open(FilePath)其中OpenWB被声明为Workbook对象。这工作得很好,甚至可以在没有询问的情况下打开宏文件?但是,当我打开Visual-Basic编辑器(Alt-F11)时,
然后我编辑数据。然后使用以下命令保存:
' Save File
OpenWB.Save代码试图保存,但随后就中断了。我已尝试启用和禁用Application.DisplayAlerts (真/假)。
问题是,我不能在VB编辑器打开的情况下运行它,因为我需要禁用宏警告,因为我有1000+文件。然而,如果不打开它,代码就不会通过保存代码行。
代码的一部分示例:
'Path
FilePath = FL.Cells(FileListCount, 1).Value
' Begin first loop
While FilePath <> ""
' Alerts?
Application.DisplayAlerts = False
' Open File
Set OpenWB = Workbooks.Open(FilePath)
' Alerts
Application.DisplayAlerts = True
' Enter Replacement Loop
ReplacementCount = 1
ID = Ctrl.Cells(ReplacementCount + 11, 2).Value
While ID <> ""
OpenWB.Sheets(Ctrl.Cells(ReplacementCount + 11, 4).Value).Range(Ctrl.Cells(ReplacementCount + 11, 5).Value).Value = Ctrl.Cells(ReplacementCount + 11, 3).Value
ReplacementCount = ReplacementCount + 1
ID = Ctrl.Cells(ReplacementCount + 11, 2).Value
Wend
' Save File
OpenWB.Save
' Close file
OpenWB.Close
' status
FL.Cells(FileListCount, 2).Value = "***** UPDATED *****"
' Increment counter
FileListCount = FileListCount + 1
' Call new path
FilePath = FL.Cells(FileListCount, 1).Value
Wend发布于 2014-01-10 00:03:06
这是未经测试的。
您已经尝试过:
Workbooks.Open(FilePath)
Set OpenWB = ActiveWorkbook.ActiveSheet
'ur goes here code
OpenWB.Activate
ActiveWorkbook.Close SaveChanges:=True它将同时消除保存行和关闭行。
https://stackoverflow.com/questions/21024590
复制相似问题