Renaming VBA created forms after having deleted the first one with the same name

vimmi

New Member
Joined
Apr 30, 2018
Messages
3
Hi,

The aim was to have VBA create a new form upon request, rename it for ease of use in other code, collect data etc then delete it after to keep things clean.
Issue, although the occurrence if it happening are slim, is when the request is done twice and the code tries to rename the form with the same name. I get a "Path/File access error".

I'm guessing it's an excel issue with how it handles objects and names.

Code:
Sub create_get_mth_form()
    Dim frm As Object
    Set frm = ThisWorkbook.VBProject.VBComponents.Add(3)
    Debug.Print frm.Name
    frm.Name = "Get_Mth2"
End Sub

Although I'm now planning on adding a check to see if the form is there in the first place and only delete it on file close I would still be curious if there is anything that could be done and why it's happening?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,371
Office Version
  1. 365
Platform
  1. Windows
I believe it some sort of bug. But you might be able to work around it by simply saving the worokbook before renaming it...

Code:
ThisWorkbook.Save
 

Forum statistics

Threads
1,170,958
Messages
5,872,989
Members
432,955
Latest member
minhnhat2504

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top