简体   繁体   中英

Userform on excel worksheet dissappears when excel sheet has activeX controls! Why

I am new to this forum. Please bear with me.
I have been working on a project in which I plan to have a worksheet that contains a userform and some activeX controls. The activeX controls are on the worksheet. I have duplicated the problem I'm facing with the script below. When subroutine finishes, the userform is not visible on the screen. If I comment-out the lines pertaining to the OLEObject, then I can see the userform as modeless on the screen. The question is, is this behavior expected? Why does the userform fail to be visible when there are activeX on the worksheet?

Sub DemoFailure()
Dim myOleObj As OLEObject
Dim myRng As Range

Set myRng = ThisWorkbook.Sheets("Sheet1").Range("C4")
ThisWorkbook.Sheets("Sheet1").Select
ThisWorkbook.Sheets("Sheet1").Activate
With ActiveSheet
    myRng.RowHeight = 20
    Set myOleObj = .OLEObjects.Add(ClassType:="Forms.CheckBox.1",  DisplayAsIcon:=False, Left:=myRng.Left + 2, Top:=myRng.Top + 2,  Width:=myRng.Width - 4, Height:=myRng.Height - 4)
    With myOleObj
        '.Object.Caption =
        .Name = "CheckBox" & CStr(ii)
    End With
End With
UserForm1.Show vbModeless
End Sub

This code works for me (assuming "UserForm1" exists in the same module). This is a very rough recreation of your code, because I don't know from your question exactly what you are trying to achieve other than to get the box and Form to display at the same time:

Sub Macro2()

Dim myOleObj As OLEObject
Dim myRng As Range
Dim ws As Worksheet

Set ws = ActiveSheet

Set myRng = ws.Range("C4")
myRng.RowHeight = 20

ws.OLEObjects.Add "Forms.CheckBox.1", _
    Left:=myRng.Left + 2, _
    Top:=myRng.Top + 2, _
    Width:=myRng.Width - 4, _
    Height:=myRng.Height - 1 _

Set myOleObj = ws.OLEObjects(1)

With myOleObj
    '.Object.Caption =
    .Name = "CheckBox" & CStr(ii)
End With

UserForm1.Show vmModeless

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM