excel 将多个MsgBox合并为一个

f4t66c6m  于 2022-12-14  发布在  其他
关注(0)|答案(2)|浏览(178)

我试图合并多个msgbox,但我无法弄清楚。这里是我的代码:

If InStr(ThisWorkbook.Worksheets("DailyReport").Range("F8").Value, "DCT") > 0 Then
    If IsEmpty(Sheet2.Range("G34").Value) Then
        MsgBox ("The Following Test is not Performed " & Sheet2.Range("E34").Value)
    End If

    If IsEmpty(Sheet2.Range("G35").Value) Then
        MsgBox ("The Following Test is not Performed " & Sheet2.Range("E35").Value)
    End If

    If IsEmpty(Sheet2.Range("G36").Value) Then
        MsgBox ("The Following Test is not Performed " & Sheet2.Range("E36").Value)
    End If
End If

我想在工作表DailyReport的单元格F8中搜索单词DCT,如果它存在,我想查看多个单元格,如G34G35G36 ......。如果这些单元格为空,则显示消息框“未执行以下测试:一米六纳一,一米七纳一,一米八纳一......”
假设G34G35为空,则msg框应显示未执行以下测试:

Cell value in E34
Cell Value in E35

Msgbox应具有继续和取消按钮
如果用户点击“继续”,则继续sub
如果用户点击“取消”,则退出sub

dwthyt8l

dwthyt8l1#

返回消息框中的组合消息

Sub CombineMessages()
    
    Dim CheckCells() As Variant: CheckCells = Array("G34", "G35", "G36")
    Dim ValueCells() As Variant: ValueCells = Array("E34", "E35", "E36")
    
    Dim CheckString As String
    CheckString = CStr(ThisWorkbook.Worksheets("DailyReport").Range("F8").Value)
    
    Dim UntestedCount As Long, MsgString As String

    If InStr(CheckString, "DCT") > 0 Then
        Dim n As Long
        For n = LBound(CheckCells) To UBound(CheckCells)
            If IsEmpty(Sheet2.Range(CheckCells(n))) Then
                MsgString = MsgString & vbLf & "    " _
                    & CStr(Sheet2.Range(ValueCells(n)).Value)
                UntestedCount = UntestedCount + 1
            End If
        Next n
    End If
    
    If UntestedCount > 0 Then
        MsgString = "The following test" _
            & IIf(UntestedCount = 1, " is", "s are") & " not performed:" _
            & vbLf & MsgString & vbLf & vbLf & "Do you want to continue?"
        Dim Msg As Long: Msg = MsgBox(MsgString, vbQuestion + vbYesNo)
        If Msg = vbNo Then Exit Sub
    End If
 
    MsgBox "Continuing...", vbInformation
 
End Sub
mrzz3bfm

mrzz3bfm2#

我想查看多个单元格,如G34、G35、G36 ....
如果这些单元格为空,则显示消息框“未执行以下测试:E34、E35、E36 ......”
G34,G35,G36....看起来这个范围是动态的?还是总是这3个?如果它是动态的,那么你是如何决定范围的。例如,为什么G34而不是G1?或者你想检查到哪里?到G的最后一个单元格?所有这些都将决定你如何编写简洁的vba代码。假设您要检查G列中的最后一个单元格。如果是从G34G60(* 仅为示例 *),则将For Next循环从For i = 34 To lRow更改为For i = 34 To 60
这就是您正在尝试的吗?(UNTESTED

Option Explicit

Sub Sample()
    Dim i As Long
    Dim lRow As Long
    Dim CellAddress As String
    
    If InStr(ThisWorkbook.Worksheets("DailyReport").Range("F8").Value, "DCT") > 0 Then
        
        With Sheet2
            '~~> Find last row in Col G
            lRow = .Range("G" & .Rows.Count).End(xlUp).Row
            
            '~~> Check the range for blank cells
            For i = 34 To lRow
                If Len(Trim(.Range("G" & i).Value2)) = 0 Then
                    CellAddress = CellAddress & "," & "E" & i
                End If
            Next i
        End With
        
        '~~> Check if any addresses were found
        If CellAddress <> "" Then
            CellAddress = Mid(CellAddress, 2)
            
            Dim ret As Integer
            
            '~~> Ask user. There is no CONTINUE button. Use YES/NO
            ret = MsgBox("The following Test is Not performed:" & _
                          vbNewLine & CellAddress & vbNewLine & _
                          "Would you like to continue?", vbYesNo)
            If ret = vbYes Then
                '~~> Do what you want
            Else
                '~~> You may not need the else/exit sub part
                '~~> Depending on what you want to do
                Exit Sub
            End If
            '
            '
            '~~> Rest of the code
            '
            '
        End If
    End If
End Sub

相关问题