vba 将用户表单直接放在excel表上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5904696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:27:35  来源:igfitidea点击:

Put a user form directly on the excel sheet

excelvba

提问by Rushui Guan

Is there any way to insert a user form directly on the excel sheet?

有没有办法直接在excel表上插入用户表单?

We can add the build-in controls as well as active x controls. I don't see why we cannot add user forms within the same workbook.

我们可以添加内置控件以及活动 x 控件。我不明白为什么我们不能在同一个工作簿中添加用户表单。

Thanks

谢谢

采纳答案by Dick Kusleika

No, I don't think it's possible.

不,我认为这不可能。

Userforms are merely containers to hold your ActiveX controls. Spreadsheets are also ActiveX control containers, so I'm not sure what the benefit of having a container in a container would be.

用户窗体只是保存 ActiveX 控件的容器。电子表格也是 ActiveX 控件容器,所以我不确定在容器中放置容器有什么好处。

You could easily color a group of cells to look like a userform and place ActiveX controls within that range. That would simulate a userform embedded on a spreadsheet. You'd be missing userform level events and probably a few other things. But if you wanted those things, you'd probably just use a userform.

您可以轻松地为一组单元格着色以使其看起来像一个用户窗体并将 ActiveX 控件置于该范围内。这将模拟嵌入在电子表格中的用户表单。您可能会错过用户表单级别的事件以及其他一些事情。但是如果你想要这些东西,你可能只需要使用一个用户表单。

If there's something you want to do that I'm missing, let me know.

如果您有什么想做的事情而我遗漏了,请告诉我。

回答by Bob22

Place this in the WorkSheet module VB:

将其放在 WorkSheet 模块 VB 中:

Private Sub Worksheet_Activate() UserForm1.Show End Sub

Private Sub Worksheet_Activate() UserForm1.Show End Sub

回答by Riley Carney

I realize it's an old post and this solution might not have been available in the past but you can insert an ActiveX control element and have "Microsoft Forms 2.0 Frame" to control the information.

我意识到这是一篇旧帖子,此解决方案过去可能不可用,但您可以插入 ActiveX 控件元素并使用“Microsoft Forms 2.0 Frame”来控制信息。

You will also need to create a custom class to handle the button presses since you can't attach macros directly onto them like a normal button.

您还需要创建一个自定义类来处理按钮按下,因为您不能像普通按钮一样将宏直接附加到它们上。

Here is an example of mine:

这是我的一个例子:

buttonEventHandler

按钮事件处理程序

Option Explicit

Public Sub Click(Sender As Integer)

End Sub

xButton

按钮

Private WithEvents btn As MSForms.commandButton
Private bEventHandler As buttonEventHandler
Private b As Integer

Public Sub createObject(EventHandlerOf As MSForms.commandButton, EventHandler As buttonEventHandler, xB As Integer)
    Set btn = EventHandlerOf
    Set bEventHandler = EventHandler
    b = xB
End Sub

Private Sub btn_Click()
    If Not bEventHandler Is Nothing Then bEventHandler.Click (b)
End Sub

in Microsoft Excel Objects: thisWorkbook

在 Microsoft Excel 对象中:thisWorkbook

Public Sub buttonEventHandler_Click(Sender As Integer)
Select Case Sender
Case 1
    'Do Stuff
End Select
End Sub

Private Sub workbook_open()
Sheet1.Frame1.Activate
Set xbtn = New Collection
Dim o As Object

Dim xB As New XButton
xB.createObject Sheet1.Frame1.Controls("excelCommandButton"), ThisWorkbook, 1
xbtn.Add xB
Set xB = Nothing
End Sub

回答by jpinto3912

You could place MyForm.Show code on the Workbook.Open event to launch the form whenever you open the file... that I've done. You can even choose where to place the form.

您可以将 MyForm.Show 代码放在 Workbook.Open 事件上,以便在您打开文件时启动表单……我已经完成了。您甚至可以选择放置表单的位置。

Your wish, if possible (I'm pretty positive it's not), could trigger concurrent VB code: e.g. auto-calculate of cells, and the form code. That's an issue because XL is single VBA thread, hence it's impossible to have two things happening. Either the form is showing and it's code is running, or XL application is running and doing cells/graphs/etc stuff.

如果可能的话(我很肯定它不是),您的愿望可以触发并发 VB 代码:例如自动计算单元格和表单代码。这是一个问题,因为 XL 是单 VBA 线程,因此不可能发生两件事。要么表单正在显示并且它的代码正在运行,要么 XL 应用程序正在运行并执行单元格/图形/等操作。