I am trying to delete an ActiveX OLEObject from a sheet and then launch a UserForm as vbModeLess.
In the code below:
First, a sub creates a OLEObject (a Label) in sheet "mySheet" with its caption and name.
Sub creteOleo()
Dim rng As Range
Set rng = Sheets("mySheet").Range("B2:C3")
With Sheets("mySheet").OLEObjects.Add(ClassType:="Forms.Label.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=rng.Left, _
Top:=rng.Top, _
Width:=rng.Width, _
Height:=rng.Height)
.Name = "myOleo" 'A name
.Object.Font.Size = 13
.Object.Caption = "test"
End With
End Sub
Then a sub that deletes it, then launches a UserForm (the UserForm just contains a label, it's for testing so there is no code inside) with vbModeLess state.
Sub deleteOleo()
Sheets("mySheet").OLEObjects("myOleo").Delete
UserForm1.Show vbModeless
End Sub
Here is the problem, deleting a specific OLEObject then launching the UserForm deletes the OLEObject as expected but then the UserForm appears for a brief moment before disappearing.
This problem only appears when UserForm is launched in vbModeLess state, so I went for another way (hiding the object instead of deleting it) that works, but I am curious to know why VBA behaves that way.
My understanding is that since Excel executes the UserForm1.Show after finishing deleting the OLEObject, it shouldn't interfere with it. Moreover, it isn't like if I was deleting all objects, but just a specific one.
