excel - 读取动态用户窗体复选框并填充数组
问题描述
我有一个动态填充的用户表单 -
For Each J In Temp
Set Cbx = UserForm1.Controls.Add("Forms.CheckBox.1")
Cbx.Caption = J
Cbx.Left = 15
Cbx.Top = 15 + (17.5 * (Cntr - 1))
Cntr = Cntr + 1
Next J
Cntr = 15 + (17.5 * (Cntr - 1)) + 50
UserForm1.CommandButton1.Top = Cntr - 35
从代码片段的最后一行可以看出,我还有一个命令按钮。
当我单击命令按钮时,我想用Caption
选中的复选框填充一个数组。
Private Sub CommandButton1_Click()
Call Summary
End Sub
Sub Summary()
Dim Num As Integer
Dim I As Integer
Dim FltrTypes() As Double
Num = UserForm1.Controls.Count - 1
ReDim Preserve FltrTypes(0)
For I = 0 To (Num - 1)
If Left(UserForm1.Controls(I).Name, 8) = "CheckBox" Then
If UserForm1.Controls(I).Value = "True" Then
FltrTypes(I) = UserForm1.Controls(I).Caption
End If
End If
Next I
End Sub
但是数组没有填充。我哪里错了?
解决方案
问题是这样的:
If UserForm1.Controls(I).Value = "True" Then
不带引号
If UserForm1.Controls(I).Value = True Then
FltrTpye
不过一定String
不会Double
有很多小错误,代码改写:
Dim Num As Integer
Dim I As Integer
Dim FltrTypes() As String
Dim NFltrTypes As Long
Num = UserForm1.Controls.Count - 1
Erase FltrTypes
NFltrTypes = 0
For I = 0 To (Num - 1)
If Left(UserForm1.Controls(I).Name, 8) = "CheckBox" Then
If UserForm1.Controls(I).Value = True Then
ReDim Preserve FltrTypes(NFltrTypes)
NFltrTypes = NFltrTypes + 1
FltrTypes(NFltrTypes - 1) = UserForm1.Controls(I).Caption
End If
End If
Next I
推荐阅读
- java - Spring Data REST 控制器不得在类级别上使用 @RequestMapping,因为这会导致 Spring MVC 的双重注册
- ejbca - 将 EJBCA 安装为 CA
- sql - 选择某些列相同但其他列不同的行
- macos - 无法打开rabbitmq localhost UI
- algorithm - 无限迷宫生成算法
- bloomberg - 是否可以在彭博社中批量下载财报电话会议记录和音频
- asp.net-core - 在 Asp.Net Core 5.0 中注册 HttpClient 时注入 ILogger
- git - 如何将 AzureDevops 配置为仅在代码提交中的特定分支上提交
- python - Python:matplotlib.pyplot.show() 未显示绘图
- reactjs - Algolia InstantSearch 没有响应