在 MsgBox 中有条件地显示多个变量

Conditionally display multiple variables in MsgBox

这是大学作业。

我们在用户表单中有这些复选框。

假设我有三个复选框,它们是:
求最大值,
查找最小值,
求平均值

这些查找特定范围内的最大值、最小值和平均值。
我的问题在于 MsgBox。
程序根据选择进行计算后,会有一个MsgBox显示这些值。

如何创建显示我之前选择的选项的 MsgBox?

如果我可以为每个选择创建一个 MsgBox,那会更容易,但是这个作业要求它们都出现在一个 MsgBox 中。

如果我只选择最大值和最小值,那么 MsgBox 应该只显示最大值和最小值。如果我只选择 Max,那么 MsgBox 应该显示 Max 值。如果我选择全部,那么 MsgBox 应该显示所有这些。

我想我可以为所有可能的情况创建一个 MsgBox,但实际上我在这里有六个选项,无论它们是否在用户窗体中被选中,它们都应该显示。我觉得这不是很有效。我想用户窗体复选框和 MsgBox 之间一定有一些条件编码。

试试下面的方法:

Sub ShowResults()

    Dim sMessage As String

    ' btnMax btnMin btnAvg - use your controls names
    If btnMax.Value = True Then
        sMessage = sMessage & "Max is: " & CStr(yourMAXvalue) & vbNewLine
    End If

    If btnMin.Value = True Then
        sMessage = sMessage & "Min is: " & CStr(yourMINvalue) & vbNewLine
    End If

    If btnAvg.Value = True Then
        sMessage = sMessage & "Avg is: " & CStr(yourAvgvalue) & vbNewLine
    End If



    If Len(sMessage) > 0 Then MsgBox sMessage
End Sub
  1. 使用(名称)创建用户表单:fmrCalculator & 标题:计算器
  2. 创建 3 个带有标题的标签:最大值、最小值和平均值
  3. 使用(名称)创建 3 个复选框:cbxMax、cbxMin 和 cbxAverage
  4. 创建 1 个带有(名称)的命令按钮:cdbCalculate & 标题:Calculate
  5. 双击 CommadButton 并导入以下代码:

代码:

Private Sub cdbCalculate_Click()

    Dim rng As Range, cell As Range
    Dim cnrtl As Control
    Dim strMessageBox As String

    'Clear the string
    strMessageBox = ""

    'Set the rng you want
    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")

    'Check if all values in the range are numeric to avoid errors
    For Each cell In rng

        If Not IsNumeric(cell) Then
            MsgBox "The value in column " & cell.Column & " , row " & cell.Row & " is not a number!", vbCritical
            'Stop the code
            End
        End If

    Next cell

    'Loop all Controls
    With Me

        For Each cnrtl In .Controls

            'Check if the control is checkbox
            If TypeName(cnrtl) = "CheckBox" Then

                'Check if the checkbox is checked and find the Max
                If cnrtl.Name = "cbxMax" And cbxMax.Value = True Then
                    strMessageBox = "Max value=" & Application.WorksheetFunction.Max(rng)
                End If

                'Check if the checkbox is checked and find the Min
                If cnrtl.Name = "cbxMin" And cbxMin.Value = True Then
                    If strMessageBox = "" Then
                        strMessageBox = "Min value= " & Application.WorksheetFunction.Min(rng)
                    Else
                        strMessageBox = strMessageBox & vbNewLine & "Min value= " & Application.WorksheetFunction.Min(rng)
                    End If
                End If

                'Check if the checkbox is checked and find the Avarage
                If cnrtl.Name = "cbxAverage" And cbxAverage.Value = True Then
                    If strMessageBox = "" Then
                        strMessageBox = "Average value=" & Application.WorksheetFunction.Average(rng)
                    Else
                        strMessageBox = strMessageBox & vbNewLine & "Avarage value= " & Application.WorksheetFunction.Average(rng)
                    End If
                End If

            End If

        Next cnrtl

        MsgBox strMessageBox, , "Results:"

    End With

    Unload Me

End Sub

用户窗体视图: