you*_*t13 6 macos excel vba macos-high-sierra
我看到将用户表单添加到 Excel 2018 for MacOS(或自 Excel 2016 起)的功能与 Excel 2011 不同。
当我说“添加用户表单”时,我指的是“UI”设计器,它允许设计按钮、框、列表。(实际上似乎只有在 Windows 版本的 Excel 2018 上才可以添加用户表单。)
我正在寻求使用 Excel 2018 for MacOS 构建一个简单的用户表单。
如果“UI”设计器不可用,我是否可以仅使用 VBA 代码源直接对用户表单进行编码(可以直接编码设计)吗?
小智 5
Excel for Mac 中以编程方式生成的 UserForm 对象的屏幕截图 - Microsoft 365
必须通过对与 ThisWorkbook 对象关联的 VBProject 的 VBComponents 集合调用 Add() 方法来生成用户窗体对象,如下所示:
Set objForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
Run Code Online (Sandbox Code Playgroud)
这创建了一个名为 UserForm1 的用户表单对象。我简要地看到了可视化编辑器,并且能够为新创建的表单拖放标签控件和命令按钮,但随后的尝试失败了。
所以我在 UserForm_Initialize() 事件过程中添加了代码,手动定位和配置现有控件。我还在自动生成的 CommandButton1_Click() 事件过程存根中添加了代码。
Option Explicit
Private Sub CommandButton1_Click()
MsgBox prompt:="Bye for now!"
Unload Me
End Sub
Private Sub UserForm_Initialize()
Me.Height = 200
Me.Width = 500
Me.Caption = "UserForm On MacOS!!"
With Me.CommandButton1
.Top = 10
.Left = 400
.Width = 50
.Height = 30
.Caption = "OK!"
.Font.Size = 20
.Font.Bold = True
End With
With Me.Label1
.Caption = "Hallelujer!"
.Width = 120
.Height = 30
.Left = 5
.Top = 10
.BorderStyle = fmBorderStyleSingle
.SpecialEffect = fmSpecialEffectEtched
With .Font
.Name = "Arial"
.Size = 20
.Bold = True
End With
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
该表单是通过附加到功能区上的自定义按钮的宏调用的。
Public Sub MakeForm()
Dim objForm As UserForm
'Execute the following statement once for each userform object to be created
'and then disable it
'Set objForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
'Display the userform
UserForm1.Show
End Sub
Run Code Online (Sandbox Code Playgroud)
这似乎表明可以将 UserForm 控件插入到 VBProject 中。
它还表明用户窗体支持的基础确实存在于 Excel for Mac 中,但它们尚未完全实现。
| 归档时间: |
|
| 查看次数: |
13550 次 |
| 最近记录: |