如何在VBA中动态调整Excel用户窗体的大小
在Excel 2019中,我想从以下文件名中获取人名:如何在VBA中动态调整Excel用户窗体的大小,excel,vba,resize,userform,Excel,Vba,Resize,Userform,在Excel 2019中,我想从以下文件名中获取人名:Summer Lovin'-John Travolta&Olivia Newton John,或者:Eddie Rabbitt在1982年与Crystal Gayle唱了二重唱 我已经动态创建了一个用户表单,这样我就可以选择有效的名称并将它们添加到电子表格的列表中 但是,我还没有找到一个有效的解决方案来更改userform的大小以适应标签和复选框 你知道我需要做什么吗?我愿意接受所有建议 Option Explicit Sub Splits
Summer Lovin'-John Travolta&Olivia Newton John
,或者:Eddie Rabbitt在1982年与Crystal Gayle唱了二重唱
我已经动态创建了一个用户表单,这样我就可以选择有效的名称并将它们添加到电子表格的列表中
但是,我还没有找到一个有效的解决方案来更改userform的大小以适应标签和复选框
你知道我需要做什么吗?我愿意接受所有建议
Option Explicit
Sub SplitstrFNForNames()
Dim strFN, substr, substr1, substr2 As String
Dim i, n As Integer
Dim MyUserForm As VBComponent
Dim chkBox As MSForms.CheckBox
Dim Label1 As MSForms.Label
ThisWorkbook.Save
If Cells(ActiveCell.Row, "B") = "" Then
strFN = "Summer Lovin' – John Travolta & Olivia Newton-John"
Else
strFN = Cells(ActiveCell.Row, "B")
End If
' Check whether the userform form exists
For n = 1 To ActiveWorkbook.VBProject.VBComponents.Count
If ActiveWorkbook.VBProject.VBComponents(n).Name = "MsgboxFNSplit" Then
ShowMsgbox
Exit Sub
Else
End If
Next n
' Make a userform
Set MyUserForm = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With MyUserForm
On Error Resume Next
.Name = "MsgboxFNSplit"
.Properties("Caption") = "Get performers names from filename"
End With
Set Label1 = MyUserForm.Designer.Controls.Add("Forms.label.1", "Label_1", True)
With Label1
.Caption = "Check names to be added to performers list"
.Left = 5
.Top = 5
.Width = 144
End With
' Add checkboxes to userform
i = 1
Do
substr1 = Left(strFN, InStr(1, strFN, " ") - 1)
strFN = Replace(strFN, substr1 & " ", "")
If InStr(1, strFN, " ") = 0 Then
substr2 = strFN
Else
substr2 = Left(strFN, InStr(1, strFN, " ") - 1)
End If
substr = substr1 & " " & substr2
Set chkBox = MyUserForm.Designer.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i, True)
chkBox.Caption = substr
chkBox.Left = 5
chkBox.Top = Label1.Height + 5 + ((i - 1) * 20)
i = i + 1
Loop Until InStr(1, strFN, " ") = 0
' Calculate height & width of userform based on sizes of labels and checkboxes
Dim h, w
Dim c As Control
h = 0: w = 0
For Each c In MyUserForm.Controls
If c.Visible Then
If c.Top + c.Height > h Then h = c.Top + c.Height
If c.Left + c.Width > w Then w = c.Left + c.Width
End If
Next c
If h > 0 And w > 0 Then ' <<< This is not working
With MyUserForm
.Width = w + 40
.Height = h + 40
End With
End If
ShowMsgbox
' Remove userform
With ActiveWorkbook.VBProject
.VBComponents.Remove .VBComponents("MsgboxFNSplit")
End With
End Sub
Sub ShowMsgbox()
MsgboxFNSplit.Show
End Sub
选项显式
子拆分strFnforNames()
Dim strFN、substr、substr1、substr2作为字符串
作为整数的Dim i,n
将MyUserForm设置为VBComponent
将chkBox设置为MSForms.CheckBox
将标签1的尺寸标注为MSForms.Label
此工作簿。保存
如果单元格(ActiveCell.Row,“B”)=“”则
strFN=“夏季爱情”-约翰·特拉沃尔塔和奥利维亚·牛顿·约翰”
其他的
strFN=单元格(ActiveCell.Row,“B”)
如果结束
'检查用户表单是否存在
对于n=1到ActiveWorkbook.VBProject.VBComponents.Count
如果ActiveWorkbook.VBProject.VBComponents(n).Name=“MsgboxFNSplit”,则
ShowMsgbox
出口接头
其他的
如果结束
下一个
'创建一个userform
设置MyUserForm=ActiveWorkbook.VBProject.VBComponents.Add(vbext\u ct\u MSForm)
使用MyUserForm
出错时继续下一步
.Name=“MsgboxFNSplit”
.Properties(“标题”)=“从文件名获取执行者名称”
以
Set Label1=MyUserForm.Designer.Controls.Add(“Forms.label.1”、“label\u 1”,True)
带标签1
.Caption=“检查要添加到执行者列表中的姓名”
.左=5
.Top=5
.宽度=144
以
'将复选框添加到userform
i=1
做
substr1=左(strFN,InStr(1,strFN,“”)-1)
strFN=Replace(strFN,substr1&“”,“”)
如果InStr(1,strFN,“”)=0,则
substr2=strFN
其他的
substr2=左(strFN,InStr(1,strFN,“”)-1)
如果结束
substr=substr1&“”和substr2
设置chkBox=MyUserForm.Designer.Controls.Add(“Forms.CheckBox.1”、“CheckBox\u”&i,True)
chkBox.Caption=substr
chkBox.Left=5
chkBox.Top=标签1.高度+5+((i-1)*20)
i=i+1
循环直到InStr(1,strFN,“”)=0
'根据标签和复选框的大小计算userform的高度和宽度
暗h,w
dimcas控件
h=0:w=0
对于MyUserForm.Controls中的每个c
如果c.可见,则
如果c.顶部+c.高度>h,则h=c.顶部+c.高度
如果c.左+c.宽度>w,则w=c.左+c.宽度
如果结束
下一个c
如果h>0和w>0,那么代码中(至少)有两个错误,但是您没有看到它们,因为您不幸地用错误恢复下一步的邪恶语句隐藏了它们
(1) 要在设计时访问表单的控件,需要通过设计器
-对象访问它们:
For Each c In MyUserForm.Designer.Controls
(2) 要设置窗体的宽度和高度,请使用.Properties
:
With MyUserForm
.Properties("Width") = w + 40
.Properties("Height") = h + 40
End With
始终使用选项Explicit
。如果您这样做,您将看到Dim h,w
并没有将变量声明为特定类型,而是默认类型Variant
,这是一种全面的类型。由于h,w
是变量,Excel无法对其进行计算,因此h>0
失败。将它们都声明为Long
Dim h as Long,w as Long
@Scott Holtzman:你说得对,h和w不应该是Variant
。然而,VBA可以对它们进行数学运算——通过0
初始化,它们得到数字。准确地说,它们应该声明为Double
,而不是Long
。非常感谢!这很有效。我删除了关于错误恢复下一步的语句。