在Excel中为表单指定一个名称

时间:2015-06-18 13:20:54

标签: excel forms vba excel-vba

目前我有一个包含大约20个表单的项目,有时我想对它们进行小的调整。所以我创建了一段代码来删除表单,然后按照我想要的方式重新创建它们。

问题是一行代码一直给我路径/文件访问错误(错误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

有人可以告诉我如何确保此错误不再出现?顺便说一下,当我手动想要在宏发生故障后更改表单名称时,也会出现此错误,但在宏发生故障之前没有这样做。

PS:我是这个网站的新手,很抱歉,如果我犯了任何新手的错误。

1 个答案:

答案 0 :(得分:0)

在您删除了用户表单后保存工作簿似乎可以清除错误。

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

ThisWorkbook.Save

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