Calculate totals for selected checkboxes in userform using excel VBA

岁酱吖の 提交于 2020-01-07 09:23:07

问题


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.

Actual Userform

Code for Estimate button:

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

In Userform you can see P0, P1, P2, so those only will have values in the excel sheet and from there only I need to calculate totals for selected checkboxes

Excel Sheet Screenshot

Any thought? Thanks in Advance


回答1:


best way is to:

  • rename all your chekboxes to reflect task and mobile/desktop values (e.g. "US-P0|Mobile", ""US-P2|Mobile", ...")

  • loop through all controls (e.g.: For Each ctl In Me.Controls)

  • select "chekboxes" type ones (e.g. If TypeName(ctl) = "CheckBox" Then)

  • split its name into wanted strings (e.g.: task = Split(taskString, "|")(0), mobileOrDesktopOffset = IIf(Split(taskString, "|")(1) = "Mobile", 0, 1))

  • finally do proper sums

You can try this code which doesn't need to rename checkboxes as above said, (but you really shoud do that..):

Private Sub preflight_calculate_Click()
    Dim preflight_resource As Double, preflight_time As Double

    Dim taskRng As Range
    Dim taskString As Variant
    Dim task As String, mobileOrDesktopOffset As Long
    With ThisWorkbook.Sheets("Preflight")
        With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))

            preflight_resource = Val(Me.preflight_resource)
            preflight_time = Val(Me.preflight_time)
            For Each taskString In GetTasks
                task = Split(taskString, "|")(0)
                mobileOrDesktopOffset = IIf(Split(taskString, "|")(1) = "Mobile", 0, 1)
                Set taskRng = .Find(what:=task, lookat:=xlWhole, LookIn:=xlValues)
                preflight_resource = preflight_resource + taskRng.Offset(, 5 + mobileOrDesktopOffset).Value
                preflight_time = preflight_time + taskRng.Offset(, 7 + mobileOrDesktopOffset).Value
            Next
        End With
    End With

    With Me
        .preflight_resource.Text = preflight_resource
        .preflight_time.Text = preflight_time
    End With
End Sub


Function GetTasks() As Variant
    Dim ctl As Control
    Dim mobileLeft As Long, desktopLeft As Long

    With Me
        For Each ctl In .Controls
            If TypeName(ctl) = "CheckBox" Then
                If ctl.Value Then
                    GetTasks = GetTasks & " " & ctl.Parent.Caption & "-" & ctl.Caption & "|" & GetMobileOrDesktop(ctl)
                End If
            End If
        Next
    End With
    GetTasks = Split(Trim(GetTasks))
End Function


Function GetMobileOrDesktop(ctl As Control) As String
    Dim ctl1 As Control
    For Each ctl1 In ctl.Parent.Controls
        If ctl1.Caption = "Mobile" Then
            If ctl1.left = ctl.left Then
                GetMobileOrDesktop = "Mobile"
            Else
                GetMobileOrDesktop = "Desktop"
            End If
            Exit For
        End If
    Next
End Function



回答2:


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


来源:https://stackoverflow.com/questions/54966060/calculate-totals-for-selected-checkboxes-in-userform-using-excel-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!