Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在MsgBox中有条件地显示多个变量_Excel_Vba_Userform - Fatal编程技术网

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

Excel 在MsgBox中有条件地显示多个变量,excel,vba,userform,Excel,Vba,Userform,这是一项大学作业 我们在userform中有这些复选框 假设我有三个复选框: 查找最大值, 查找最小值, 求平均值 它们可以找到特定范围内的最大值、最小值和平均值。 我的问题在于MsgBox。 程序根据选择进行计算后,将有一个MsgBox显示这些值 如何创建一个MsgBox来显示我以前选择的选项 如果我可以为每个选项创建一个MsgBox,那会更容易,但是这个任务要求它们都显示在一个MsgBox中 如果我只选择了Max和Min,那么MsgBox应该只显示Max值和Min值。如果我只选择Max,那么

这是一项大学作业

我们在userform中有这些复选框

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

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

如何创建一个MsgBox来显示我以前选择的选项

如果我可以为每个选项创建一个MsgBox,那会更容易,但是这个任务要求它们都显示在一个MsgBox中

如果我只选择了Max和Min,那么MsgBox应该只显示Max值和Min值。如果我只选择Max,那么MsgBox应该显示Max值。如果我选择all,那么MsgBox应该显示所有这些内容


我想我可以为所有可能的场景创建一个MsgBox,但实际上我这里有六个选项,无论它们是否在userform中被选中,都应该显示出来。我觉得这不是很有效。我想在userform复选框和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
  • 使用(名称):fmrCalculator和标题:Calculator创建用户表单
  • 创建3个带有标题的标签:最大值、最小值和平均值
  • 使用(名称)创建3个复选框:cbxMax、cbxMin和cbxAverage
  • 创建一个命令按钮(名称):CDB计算&标题:计算
  • 双击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
    
    用户表单视图:

    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
    

    您需要连接一个构成消息正文的字符串。正文显然将包含结果复选框值。将vbcrlf用于新行以格式化文本。