【问题标题】:Excel VBA Find row based on 2 conditions and add to msgboxExcel VBA 根据 2 个条件查找行并添加到 msgbox
【发布时间】:2016-03-22 19:58:19
【问题描述】:

我有一个跟踪项目的日志表。我试图弄清楚如何查看每一行,查找 A 列中是否有 ID#,如果有,则检查该行以查看 H 列中是否有日期。如果缺少日期,则显示 msgbox将在打开时弹出以列出所有缺少日期的 ID#。定义 StrID 时我所拥有的工作,但我希望能够找到所有 ID#s 并将它们列出在 msgbox 上。任何指导或正确方向的观点将不胜感激。谢谢

    Dim rngFound As Range
    Dim strFirst As String
    Dim strid As String
    Dim strDate As String

    strid = "8"
    strDate = vbNullString

    Set rngFound = Columns("a:a").Find(strid, Cells(Rows.Count, "a:a"), xlValues, xlWhole)
    If Not rngFound Is Nothing Then
        strFirst = rngFound.Address
        Do
            If LCase(Cells(rngFound.Row, "h:h").Text) = LCase(strDate) Then
                'Found a match
                MsgBox "ID #" & Cells(rngFound.Row, "a:a").Text & "is open."

            End If
            Set rngFound = Columns("a:a").Find(strid, rngFound, xlValues, xlWhole)
        Loop While rngFound.Address <> strFirst
    End If

    Set rngFound = Nothing

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    尝试以下方法:

    Sub test()
    
    Dim i As Long
    Dim lRow As Long
    Dim msg As String
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    With ThisWorkbook.ActiveSheet
    
    For i = 2 To lRow
    
    If IsNumeric(.Cells(i, 1).Value) And .Cells(i, 1).Value <> "" Then
    
        If .Cells(i, 8).Value = "" Then
    
            If msg = "" Then
    
                msg = CStr(.Cells(i, 1).Value)
    
            Else
    
                msg = msg + ", " + CStr(.Cells(i, 1).Value)
    
            End If
    
        End If
    
    End If
    
    Next i
    
    End With
    
    MsgBox msg
    
    End Sub
    

    它遍历 A 列 (ID) 中的每个单元格,并且当不是空白 AND 是一个数字时(如果某些值不是 ID,而是文本字段),那么如果 H 列中同一行中的单元格是空白,将其添加到msg,然后在末尾显示一个消息框msg

    我的测试输出如下:

    【讨论】:

    • 这很完美,谢谢@TheGuyThatDoesn'tKnowMuch!我能够添加 CurrentDate 和每条记录的日期的差异,以在 msgbox 中的 ID 旁边显示未完成的日子。
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    相关资源
    最近更新 更多