当Excel工作表具有ActiveX控件时,Excel工作表上的用户表单就会消失!为什么 [英] Userform on excel worksheet dissappears when excel sheet has activeX controls! Why

查看:60
本文介绍了当Excel工作表具有ActiveX控件时,Excel工作表上的用户表单就会消失!为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个论坛的新手.请忍受我.
我一直在一个项目中,我计划在其中有一个包含用户窗体和一些activeX控件的工作表.activeX控件在工作表上.我已经复制了下面脚本所面临的问题.子例程完成后,用户窗体在屏幕上不可见.如果我注释掉与OLEObject有关的行,那么我可以在屏幕上看到该用户窗体为非模式.问题是,这种行为是预期的吗?当工作表上有ActiveX时,为什么用户窗体无法显示?

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

推荐答案

此代码对我有用(假设"UserForm1"存在于同一模块中).这是您的代码的非常粗略的重述,因为除了让框和Form同时显示之外,我从您的问题中不知道您到底要实现什么?

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

这篇关于当Excel工作表具有ActiveX控件时,Excel工作表上的用户表单就会消失!为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆