vba在多列中查找多个值并返回行号

时间:2015-08-27 03:54:30

标签: excel vba excel-vba excel-2010

我下面有下表,我想获取行号,例如column(A:A)= "BTYA" and Column(B:B)="2" and Column(D:D)="Plan/actual Harvest Qty"

enter image description here

下面是我的代码,它给了我第一次出现的位置而不是下一次出现的位置。任何帮助将不胜感激

Public Function Female_HarvestMaleQty_NotNull(ByVal Target As Range)
Const Col_Description = "D"
Const SheetName As String = "Test"

Dim Max_Growing_Days As Range
Dim Farm_Name As Range
Dim House_No As Range
Dim Description As Range

Dim FarmName As String
Dim HouseNo As Integer
Dim Descriptions As String
With Worksheets(SheetName)
House_name_no = .Cells(Target.Row, "A").Value & "-" &     .Cells(Target.Row,"B").Value
        With Worksheets("Farm Parameters").Rows("1:1")
            Set Max_Growing_Days = .Find(what:=House_name_no, _
            after:=.Cells(.Cells.Count), _
            LookIn:=xlFormulas, _
            lookat:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
        End With

FarmName = .Cells(Target.Row, "A").Value
        With Worksheets("Farm Parameters").Columns("A:A")
            Set Farm_Name = .Find(what:=FarmName, _
            after:=.Cells(.Cells.Count), _
            LookIn:=xlFormulas, _
            lookat:=xlWhole, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext, MatchCase:=False)
        End With
HouseNo = .Cells(Target.Row, "B").Value
        With Worksheets(SheetName).Columns("B:B")
            Set House_No = .Find(what:=HouseNo, _
            after:=.Cells(.Cells.Count), _
            LookIn:=xlFormulas, _
            lookat:=xlWhole, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext, MatchCase:=False)
        End With
Descriptions = .Cells(Target.Row, "D").Value
        With Worksheets(SheetName).Columns("D:D")
            Set Description = .Find(what:=Descriptions, _
            after:=.Cells(.Cells.Count), _
            LookIn:=xlFormulas, _
            lookat:=xlWhole, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext, MatchCase:=False)
        End With

        No_Days = Worksheets("Farm Parameters").Cells(8,   Max_Growing_Days.Column).Value - .Cells(Target.Row + 6, Target.Column).Value

 If .Cells(Target.Row, "A").Value = FarmName And .Cells(Target.Row, "B").Value = HouseNo And Description = "Plan/Actual Input DOC Qty" Then
MsgBox Description.Address

End If 
End With
End Function


1 个答案:

答案 0 :(得分:0)

您需要解决我在帖子评论中提到的问题

不知道“测试”表格包含什么,请根据需要调整此代码:

Option Explicit

Public Function Female_HarvestMaleQty_NotNull(ByVal Target As Range)
    Dim max_Growing_Days As Range, house_name_no As Long, No_Days As Long
    Dim farm_Name As Range, house_No As Range, desc As Range
    Dim farmName As String, houseNo As Long, descs As String
    Dim ws1 As Worksheet, wsFP As Worksheet, tr As Long, tc As Long, rowID As Long

    Set ws1 = Worksheets("Test")
    Set wsFP = Worksheets("Farm Parameters")
    tr = Target.Row
    tc = Target.Column

    house_name_no = 2
    With wsFP.Rows("1:1")
        Set max_Growing_Days = ws1.UsedRange.Find(What:=house_name_no, _
            After:=ws1.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
    End With

    farmName = "BTYA"
    houseNo = "2"
    descs = "Plan/actual Harvest Qty"

    No_Days = wsFP.Cells(8, max_Growing_Days.Column).Value - ws1.Cells(tr + 6, tc).Value

    rowID = getRowId(wsFP, farmName, houseNo, descs)
    MsgBox farmName & " || " & houseNo & " || " & descs & " ---> found on row: " & rowID

End Function
Public Function getRowId(ByRef ws As Worksheet, ByVal farm As String, _
                         ByVal house As String, ByVal desc As String) As Long

    Application.ScreenUpdating = False

    With ws.UsedRange
        .AutoFilter Field:=1, Criteria1:=farm
        .AutoFilter Field:=2, Criteria1:="*" & house
        .AutoFilter Field:=4, Criteria1:=desc

        If .SpecialCells(xlCellTypeVisible).Areas.Count > 1 Then
            getRowId = .SpecialCells(xlCellTypeVisible).Areas(2).Row
        End If

        .AutoFilter
    End With

    Application.ScreenUpdating = True

End Function
Public Sub testRow()
    Female_HarvestMaleQty_NotNull Worksheets("Test").Range("A5")
End Sub