我使用同时打开的多个工作簿来跟踪我撰写的文章。这两个工作簿都有各自的宏和编辑其数据的用户窗体,其中之一具有无模式的用户窗体。如何确保无模式用户窗体在正确的工作簿上进行更改。我尝试引用Workbooks(“ Published Articles Tracker v3.0.xlsm”)。Sheets(Sheet2).Activate,但它总是返回错误。我想继续使用workbooks()方法来引用我的工作簿,但我认为问题出在我文件名的格式上,但如果可能的话,我想保持这种方式。有什么想法吗?
答案 0 :(得分:0)
使用变量来指向您的工作簿和工作表-更快,更易于阅读,并且在更改名称时无需费劲。
Global ThatWorkbook as Workbook
'NB ThisWorkbook is predefined by VB to refer to whichever
'workbook is hosting the code
Sub Disambiguate()
dim wb as workbook
for each wb in workbooks
if wb.name =""Published Articles Tracker v3.0.xlsm" then
Set ThatWorkbook = wb
Exit For
End If
Next ws
If ThatWorkbook is Nothing then
MSGBOX "File Not Found",vbOkOnly,"No Workbook"
End IF
End sub