如果您可以控制每个工作簿,则可以在每个工作簿中创建一个名为“已检查”的命名范围,并按如下方式更改您的代码...
Sub OpenAllWorkbooksnew()
Set destWB = ActiveWorkbook
Dim DestCell As Range
Dim cwb As Workbook
For Each cwb In Workbooks
'ensure "Checked" flag is reset
cwb.Names("Checked").RefersToRange.Value = "Unchecked"
'bring workbook to focus
cwb.Activate
'wait until "Checked" flag is changed to "OK"
Do Until cwb.Names("Checked").RefersToRange.Value <> "Unchecked"
DoEvents
Loop
'check result and action accordingly
If UCase(cwb.Names("Checked").RefersToRange.Value) = "OK" Then
ActiveWorkbook.Close True
ElseIf UCase(cwb.Names("Checked").RefersToRange.Value) = "ERROR" Then
ActiveWorkbook.Close False
End If
Next cwb
End Sub
如果您对循环通过的每个工作簿没有这样的控制级别,则可以使用相同的方法,但使用功能区/命令栏(取决于您的 Excel 版本)。如果没有关于如何准确调用此代码等的额外信息,就很难再具体化了。