在Excel中为表单提供一个名称

目前我有一个有20个表格的项目,有时候我想对它们进行一些小的调整。 所以我创build了一段代码来删除表单,然后以我想要的方式重新创build它们。

问题是一行代码不断给我path/文件访问错误(错误75)

这是一小段代码:

Sub makeForm(formName As String) Dim form As Object 'These lines delete the old form Set form = ThisWorkbook.VBProject.VBComponents(formName) ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=form 'This line creates the new form Set form = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm) 'These lines give the new form a few properties With form .Properties("Name") = formName 'This is the line of code that gives the error .Properties("Caption") = formName .Properties("Width") = 320 .Properties("Height") = 242 End With End Sub 

有人可以告诉我如何可以确保这个错误不再出现? 顺便说一句,这个错误也出现,当我手动要改变一个窗体的名称后,这个macros失败,但不是当我这样做之前,macros失败。

PS:我对这个网站很陌生,如果我犯了新人的错误,我很抱歉。

删除用户窗体后保存工作簿似乎清除错误。

 'These lines delete the old form Set form = ThisWorkbook.VBProject.VBComponents(formName) ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=form Set form = Nothing ThisWorkbook.Save 

我猜Excel保存时会刷新一些内部值。