无法根据用户窗体中的复选框选择添加单元格值

时间:2019-03-04 06:09:48

标签: excel vba

我正在写一个用户表单

我要实现的目标:在运行具有多个选择复选框的用户窗体时。

  1. 收集所有选中的复选框标题及其父框架名称
  2. 在第一列中使用收集到的字符串过滤数据库
  3. 遍历过滤后的单元并得出所需的总和
  4. 所选内容可以包含每行不同的列(基于复选框选择)

请参阅下面的示例以更好地理解

Excel sheet snapshot which contains data

Userform

估算按钮

编码
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

0 个答案:

没有答案