-1

好吧,我在一分钟前解决了一个小问题,但现在我的宏上有另一个问题:(

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim S1 As String, S2 As String
    Dim S3 As String, S4 As String
    Dim lRow As Long, i As Long
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1")

    S1 = "Football"
    S2 = "Basket"

    S3 = "Sport1"
    S4 = "Sport2"

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To lRow
            If Len(Trim(.Range("E" & i).Value)) = 0 Then
                Select Case .Range("C" & i).Value
                    Case S1, S2

                        MsgBox "Insert value in the cell " & _
                        .Range("E" & i).Address

                        Cancel = True

                        Exit For
                End Select
            End If

            If (Len(Trim(.Range("F" & i).Value)) = 0) Or _
               (Len(Trim(.Range("G" & i).Value)) = 0) Or _
               (Len(Trim(.Range("H" & i).Value)) = 0) Then
                Select Case .Range("C" & i).Value
                    Case S3, S4

                        MsgBox "Insert value in the cell " & _
                        .Range("F" & i).Address, _
                        .Range("G" & i).Address, _
                        .Range("H" & i).Address

                        Cancel = True

                        Exit For
                End Select
            End If
        Next i
    End With
End Sub

第一个if有效,但第二个

If (Len(Trim(.Range("F" & i).Value)) = 0) Or _
               (Len(Trim(.Range("G" & i).Value)) = 0) Or _
               (Len(Trim(.Range("H" & i).Value)) = 0) Then
                Select Case .Range("C" & i).Value
                    Case S3, S4

                        MsgBox "Insert value in the cell " & _
                        .Range("F" & i).Address, _
                        .Range("G" & i).Address, _
                        .Range("H" & i).Address

                        Cancel = True

                        Exit For
                End Select
 End If

没有。有什么不对?

如您所见,条件相同,但在不同的列中

4

1 回答 1

1

如果第一个IF有效,那么第二个将不会,因为我们正在退出 FOR 循环。

这是您正在尝试的(未测试)吗?

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim S1 As String, S2 As String
    Dim S3 As String, S4 As String, sMsg As String
    Dim lRow As Long, i As Long
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1")

    S1 = "Football": S2 = "Basket": S3 = "Sport1": S4 = "Sport2"

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To lRow
            If Len(Trim(.Range("E" & i).Value)) = 0 Then
                Select Case .Range("C" & i).Value
                    Case S1, S2
                        sMsg = .Range("E" & i).Address
                End Select
            End If

            If (Len(Trim(.Range("F" & i).Value)) = 0) Or _
               (Len(Trim(.Range("G" & i).Value)) = 0) Or _
               (Len(Trim(.Range("H" & i).Value)) = 0) Then
                Select Case .Range("C" & i).Value
                    Case S3, S4
                        If sMsg = "" Then
                            sMsg = .Range("F" & i).Address & " OR " & _
                                   .Range("G" & i).Address & " OR " & _
                                   .Range("H" & i).Address
                        Else
                            sMsg = sMsg & " OR " & _
                                   .Range("F" & i).Address & " OR " & _
                                   .Range("G" & i).Address & " OR " & _
                                   .Range("H" & i).Address
                        End If
                End Select
            End If

            If sMsg <> "" Then
                MsgBox "One or all these cells are empty. " & _
                       "Please insert value in the cell(s) " & _
                        sMsg
                Cancel = True
                Exit For
            End If
        Next i
    End With
End Sub

跟进(来自评论)

未经测试

这会将相关单元格存储在一个范围内,然后在激活相关工作表后简单地选择它。但是我不推荐这种方法。或者,您可以为单元格着色...通过代码或通过条件格式...

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim S1 As String, S2 As String
    Dim S3 As String, S4 As String, sMsg As String
    Dim lRow As Long, i As Long
    Dim ws As Worksheet
    Dim rng As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")

    S1 = "Football": S2 = "Basket": S3 = "Sport1": S4 = "Sport2"

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To lRow
            If Len(Trim(.Range("E" & i).Value)) = 0 Then
                Select Case .Range("C" & i).Value
                    Case S1, S2
                        sMsg = .Range("E" & i).Address
                        Set rng = .Range("E" & i)
                End Select
            End If

            If (Len(Trim(.Range("F" & i).Value)) = 0) Or _
               (Len(Trim(.Range("G" & i).Value)) = 0) Or _
               (Len(Trim(.Range("H" & i).Value)) = 0) Then
                Select Case .Range("C" & i).Value
                    Case S3, S4
                        If sMsg = "" Then
                            sMsg = .Range("F" & i).Address & " OR " & _
                                   .Range("G" & i).Address & " OR " & _
                                   .Range("H" & i).Address
                        Else
                            sMsg = sMsg & " OR " & _
                                   .Range("F" & i).Address & " OR " & _
                                   .Range("G" & i).Address & " OR " & _
                                   .Range("H" & i).Address
                        End If

                        If rng Is Nothing Then
                            Set rng = .Range("F" & i & ":H" & i)
                        Else
                            Set rng = Union(rng, .Range("F" & i & ":H" & i))
                        End If
                End Select
            End If

            If sMsg <> "" Then
                MsgBox "One or all these cells are empty. " & _
                       "Please insert value in the cell(s) " & _
                        sMsg

                If Not rng Is Nothing Then
                    .Activate
                    rng.Select
                End If

                Cancel = True
                Exit For
            End If
        Next i
    End With
End Sub
于 2013-09-12T10:40:40.690 回答