Excel 2018 for MacOS 用户表单
Userform with Excel 2018 for MacOS
我看到无法将用户表单添加到 Excel 2018 for MacOS(或自 Excel 2016 起)的功能与 Excel 2011[=20 不同=].
当我说“添加用户窗体”时,我指的是允许设计按钮、框和列表的“UI”设计器。 (实际上,添加用户表单似乎仅适用于 Excel 2018 的 Windows 版本。)
我正在寻求使用 Excel 2018 for MacOS 构建一个简单的用户表单。
如果“UI”设计器不可用,是否可以仅使用VBA代码源直接对用户窗体进行编码(设计可以直接编码)吗?
A screenshot of a programmatically generated UserForm object in Excel for Mac - Microsoft 365
必须通过调用与 ThisWorkbook 对象关联的 VBProject 的 VBComponents 集合上的 Add() 方法来生成用户窗体对象,如下所示:
Set objForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
这创建了一个名为 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
通过附加到功能区自定义按钮的宏调用表单。
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
这似乎表明可以将 UserForm 控件插入到 VBProject 中。
它还表明 Excel 中确实存在 Mac 用户窗体支持的基础,但它们尚未完全实现。
所以我在另一个线程上问过这个问题,但是有没有人想出让用户窗体弹出以进行编辑的代码?
setobjForm 代码打开了一个完美运行的设计器。
必须进行一些代码破解才能触发表单设计器和工具箱再次打开以编辑现有表单。
我看到无法将用户表单添加到 Excel 2018 for MacOS(或自 Excel 2016 起)的功能与 Excel 2011[=20 不同=].
当我说“添加用户窗体”时,我指的是允许设计按钮、框和列表的“UI”设计器。 (实际上,添加用户表单似乎仅适用于 Excel 2018 的 Windows 版本。)
我正在寻求使用 Excel 2018 for MacOS 构建一个简单的用户表单。
如果“UI”设计器不可用,是否可以仅使用VBA代码源直接对用户窗体进行编码(设计可以直接编码)吗?
A screenshot of a programmatically generated UserForm object in Excel for Mac - Microsoft 365
必须通过调用与 ThisWorkbook 对象关联的 VBProject 的 VBComponents 集合上的 Add() 方法来生成用户窗体对象,如下所示:
Set objForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
这创建了一个名为 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
通过附加到功能区自定义按钮的宏调用表单。
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
这似乎表明可以将 UserForm 控件插入到 VBProject 中。
它还表明 Excel 中确实存在 Mac 用户窗体支持的基础,但它们尚未完全实现。
所以我在另一个线程上问过这个问题,但是有没有人想出让用户窗体弹出以进行编辑的代码?
setobjForm 代码打开了一个完美运行的设计器。
必须进行一些代码破解才能触发表单设计器和工具箱再次打开以编辑现有表单。