Check if Userform exsists before deleting via VB

R

Rich

I have a block of code that deletes a form after I close it

ThisWorkbook.VBProject.VBComponents.Remove _
ThisWorkbook.VBProject.VBComponents("FormNameHere")

I run this code before some code that rebuilds the form with new data.
The problem happens when the user aborts the macro in between the
form being delted and being recreated. If that happens the next time
I run the macro there is no form to delete and it fails.

How can I wrap this code so that it checks to see if this form exsists
first?
Rich
 
D

Doug Glancy

Rich,

I wasn't familiar with the syntax below before reading your message but this
seems to work. If it doesn't exist it doesn't cause an error:

On Error Resume Next
ThisWorkbook.VBProject.VBComponents.Remove _
ThisWorkbook.VBProject.VBComponents("UserForm1")
On Error GoTo 0

Alternately, the following tests for the existence of the form:

Sub test()

Dim uform As Object

On Error Resume Next
Set uform = ThisWorkbook.VBProject.VBComponents("UserForm1")
If Error <> "" Then
MsgBox "no such form"
Else
MsgBox "form exists"
End If
On Error GoTo 0

End Sub

Tested in XP XL2000

hth,

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top