我正在写一个用户表单
我要实现的目标:在运行具有多个选择复选框的用户窗体时。
请参阅下面的示例以更好地理解
Excel sheet snapshot which contains data
为估算按钮
编码Private Sub preflight_calculate_Click()
Dim preflight_resource As Double, preflight_time As Double
preflight_resource = Val(Me.preflight_resource)
preflight_time = Val(Me.preflight_time)
Dim cell As Range
With ThisWorkbook.Sheets("Preflight")
With .Range("A2", .Cells(.Rows.Count, 1).End(xlUp))
.AutoFilter 1, Criteria1:=GetCheckedCaptions, Operator:=xlFilterValues
For Each cell In .SpecialCells(xlCellTypeVisible)
preflight_resource = Val(preflight_resource) + cell.Offset(, 6).Value
preflight_time = Val(preflight_time) + cell.Offset(, 8).Value
Next
End With
.AutoFilterMode = False
End With
With Me
.preflight_resource.Text = preflight_resource
.preflight_time.Text = preflight_time
End With
End Sub
Function GetCheckedCaptions() As Variant
Dim ctl As Control
With Me
For Each ctl In .Controls
If TypeName(ctl) = "CheckBox" Then
If ctl.Value Then
GetCheckedCaptions = GetCheckedCaptions & " " & ctl.Parent.Caption & "-" & ctl.Caption
End If
End If
Next
End With
GetCheckedCaptions = Split(Trim(GetCheckedCaptions))
End Function
预期结果: 例如:
如果我按如下所示选择此复选框,则US-> Mobile-> P0和US-> Desktop-> P1
输出应为:
下面的文本框:
已利用资源:(F2 + G3)->(0.73 + 0.62)-> 1.35 (在内部文本框中)
以小时为单位的时间:(H2 + I3)->(5.87 + 4.95)-> 10.82 (在内部文本框中)
如何实现?
我的Excel文件链接:(“ Preflight”是存在数据的工作表名称)用户表单将从“估算表”工作表中可见
https://drive.google.com/file/d/1K2LdVuJXNZoavrkcQgslY20c4R9nlRfo/view?usp=sharing