如果行怎么实现呢?

时间:2019-04-03 15:25:34

标签: excel vba

我无法确定这个Else或Else If语句在此IF语句块中的位置。快速调试:如果在指定行中的输入框中找到键入的值,请在我指定的工作表中填充该行中的选择数据。之后的另一个if语句依赖于另一个值(以进一步过滤结果)。

它很好用,但是我只想填充一个msg框(如果我放入的作业代码存在),但不基于第二个IF语句值。现在,它只是不填充数据,但这可能会使幻觉有些破损,而不是不符合所有条件。

我想在第二个缩进的if语句之间(在Do / while循环之后)在此处添加类似Else if .... msg box "Job code exists, but not under this EC member的内容:

    If Not rFound Is Nothing Then
        sFirst = rFound.Address

        Do
            If ThisWorkbook.Worksheets("Sheet2").Cells(rFound.Row, 5).Value = lLob Then
                sh.Cells(rw, 4) = rFound.Offset(, 0).Value
                sh.Cells(rw, 5) = rFound.Offset(, 1).Value
                sh.Cells(rw, 6) = rFound.Offset(, 3).Value
                sh.Cells(rw, 7) = rFound.Offset(, 5).Value
                sh.Cells(rw, 8) = rFound.Offset(, 6).Value
                sh.Cells(rw, 9) = rFound.Offset(, 7).Value

                rw = rw + 1

            End If
            Set rFound = .FindNext(rFound)

        Loop While rFound.Address <> sFirst

    Else
        MsgBox "Job Code [" & lJobCode & "] not eligible.", , "Error"
    End If

整个代码:

Sub tgr()

Dim rFound As Range
Dim lJobCode As String
Dim lLob As String
Dim sFirst As String
Dim sResults As String
Dim sh As Worksheet
Dim rw As Long

lJobCode = Application.InputBox("Please provide a job code", "Job Code", Type:=2)
lLob = Application.InputBox("Please select EC Member", "EC Member", Type:=2)
If lJobCode = "False" Or lLob = "False" Then Exit Sub   'Pressed cancel

Set sh = Sheets("Sheet1")
rw = 8
With ThisWorkbook.Worksheets("Sheet2").Columns("A")
    Set rFound = .Find(lJobCode, .Cells(.Cells.Count), xlValues, xlWhole)

    If Not rFound Is Nothing Then
        sFirst = rFound.Address

        Do
            If ThisWorkbook.Worksheets("Sheet2").Cells(rFound.Row, 5).Value = lLob Then
                sh.Cells(rw, 4) = rFound.Offset(, 0).Value
                sh.Cells(rw, 5) = rFound.Offset(, 1).Value
                sh.Cells(rw, 6) = rFound.Offset(, 3).Value
                sh.Cells(rw, 7) = rFound.Offset(, 5).Value
                sh.Cells(rw, 8) = rFound.Offset(, 6).Value
                sh.Cells(rw, 9) = rFound.Offset(, 7).Value

                rw = rw + 1

            End If
            Set rFound = .FindNext(rFound)

        Loop While rFound.Address <> sFirst

    Else
        MsgBox "Job Code [" & lJobCode & "] not eligible.", , "Error"
    End If

End With

End Sub

2 个答案:

答案 0 :(得分:0)

您要引入一个标志来捕获状态,该状态的含义是“找到了lJobCode,但与lJob不匹配”,然后在满足内部条件时以及遍历所有条件后将该标志设置为True搜索结果,如果标记仍为MsgBox,则弹出False

Dim matched As Boolean
If Not rFound Is Nothing Then
    sFirst = rFound.Address

    Do
        If ThisWorkbook.Worksheets("Sheet2").Cells(rFound.Row, 5).Value = lLob Then
            matched = True
            '...
        End If
    Loop While ...

    If Not matched Then MsgBox "Your new message here"

 Else
    MsgBox "Job Code [" & lJobCode & "] not eligible.", , "Error"
 End If

答案 1 :(得分:0)

With ThisWorkbook.Worksheets("Sheet2").Columns("A")
    Set rFound = .Find(lJobCode, .Cells(.Cells.Count), xlValues, xlWhole)

    If Not rFound Is Nothing Then
        sFirst = rFound.Address

        dim lobFoundCount as integer
        lobFoundCount = 0

        Do
            If ThisWorkbook.Worksheets("Sheet2").Cells(rFound.Row, 5).Value = lLob Then
                lobFoundCount = lobFoundCount + 1

                sh.Cells(rw, 4) = rFound.Offset(, 0).Value
                sh.Cells(rw, 5) = rFound.Offset(, 1).Value
                sh.Cells(rw, 6) = rFound.Offset(, 3).Value
                sh.Cells(rw, 7) = rFound.Offset(, 5).Value
                sh.Cells(rw, 8) = rFound.Offset(, 6).Value
                sh.Cells(rw, 9) = rFound.Offset(, 7).Value

                rw = rw + 1
            End If
            Set rFound = .FindNext(rFound)

        Loop While rFound.Address <> sFirst

        If lobFoundCount = 0 Then 'No rows have matching lob
            Msgbox("job code exists but not under this ec member")
        End If
    Else
        MsgBox "Job Code [" & lJobCode & "] not eligible.", , "Error"
    End If

End With