使用excel VBA计算用户表单中选定复选框的总数 [英] Calculate totals for selected checkboxes in userform using excel VBA

查看:38
本文介绍了使用excel VBA计算用户表单中选定复选框的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有多个复选框的用户表单,我想在其中计算所选复选框的/总计并显示在用户表单本身中.

I'm Creating an userform with multiple checkboxes, where I want to Calculate/totals for the selected checkboxes and to be displayed in the Userform itself.

实际用户表单

估计按钮的代码:

Private Sub preflight_calculate_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Preflight")
With ws
LastRow = .Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow
   For j = 0 To us_mp0.Selected - 1
       lbValue = us_mp0.Selected(j)

       If .Cells(i, "A").Value = lbValue Or _
          .Cells(i, "A").Value = Val(lbValue) Then
           preflight_resource = Val(preflight_resource) + Val(.Cells(i, "G").Value)
           preflight_time = Val(preflight_time) + Val(.Cells(i, "I").Value)
       End If
   Next
 Next
 End With
 End Sub

在用户表单中,您可以看到 P0、P1、P2,因此这些只有在 Excel 表中才有值,从那里我只需要计算选定复选框的总数

Excel 表格截图

有什么想法吗?提前致谢

Any thought? Thanks in Advance

推荐答案

你可以使用 class 和下面类似的东西,剩下的留给你自学:

You may use class with something like below, the rest may left for your self-learning:

Option Explicit
Public WithEvents Chk As MSForms.CheckBox

Private Sub Chk_Change()
    If Chk Then
        .ChkCount = .ChkCount + 1
    Else
        .ChkCount = .ChkCount - 1
    End If
End Sub

这篇关于使用excel VBA计算用户表单中选定复选框的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆