首页 > 解决方案 > 如何检查工作簿是否存在?

问题描述

我有一个变量(WB)。我将活动工作簿分配给该变量。

我想检查 WB 是否设置为 activeworkbook 或者它是否仍然为空。如果它存在,我将关闭工作簿。

标签: excelvba

解决方案


您可以检查对象变量是否由条件设置

if wb is nothing then .... ' not set
if not wb is nothing then .... ' is set

你的情况

if not wb is nothing then
  if wb.name = thisworkbook.name then wb.close
end if    

或在一行中

if not wb is nothing then if wb.name = thisworkbook.name then wb.close
  

推荐阅读