Excel vba - find row number where colum data (multiple clauses)

后端 未结 2 835
庸人自扰
庸人自扰 2021-01-03 06:45

I need a function in VBA that finds the row number based on 2 where clauses.

Here is the Excel sample:

**     A      B      C      D**
  1    id1             


        
2条回答
  •  长情又很酷
    2021-01-03 07:05

    In VBA you can do a brute force check like the following:

    Public Sub Test()
      Dim message As String
      Dim row As Long
      row = Find("id1", "day2")
      message = "Not Found"
      If (row > 0) Then
        message = ActiveSheet.Cells(row, 3).Value
      End If
      MsgBox message
    End Sub
    
    Function Find(aVal As String, bVal As String) As Long
      Dim maxRow As Long
      Dim row As Long
      maxRow = Range("A65536").End(xlUp).row
      For row = 2 To maxRow
        Dim a As String
        a = ActiveSheet.Cells(row, 1).Value
        b = ActiveSheet.Cells(row, 2).Value
        If a = aVal And b = bVal Then
          Find = row
          Exit Function
        End If
      Next row
      Find = -1
    End Function
    

提交回复
热议问题