【问题标题】:Conditionally display multiple variables in MsgBox有条件地在 MsgBox 中显示多个变量
【发布时间】:2019-11-02 08:44:53
【问题描述】:

这是大学作业。

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

假设我有三个复选框:
寻找麦克斯,
找敏,
求平均值

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

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

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

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

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

【问题讨论】:

  • 您需要连接一个构成消息正文的字符串。正文显然会包含生成的复选框值。使用 vbcrlf 换行来格式化文本。

标签: excel vba userform


【解决方案1】:

试试下面的方法:

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

【讨论】:

    【解决方案2】:
    1. 使用(名称)创建用户表单:fmrCalculator 和标题:计算器
    2. 创建 3 个带有标题的标签:Max、Min 和 Avarage
    3. 使用(名称)创建 3 个复选框:cbxMax、cbxMin 和 cbxAverage
    4. 使用(名称)创建 1 个命令按钮:cdbCalculate 和标题:计算
    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
    

    用户窗体视图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2023-02-09
      • 2021-08-06
      • 2023-03-22
      • 2017-04-28
      • 2015-04-24
      相关资源
      最近更新 更多