如何使用Excel宏VBA在Excel工作表中查找行?

时间:2015-03-06 11:24:09

标签: excel vba indexing counter

enter image description here

我现在的代码是:

Set RecT = gConnect.Execute("SELECT [Volume] FROM [Output$] WHERE ((([Facilities])=""IDK"") AND (([Detail_1])=""BPL"" Or ([Detail_1])=""SI"")) ORDER BY [Detail_1];")
Worksheets("Results").Cells(52, 4).CopyFromRecordset RecT

首先,使用Set Rect我在另一张表格中查找我想要的值,然后将其复制到我的表格的第52行的第4列中。结果' (图中的那个)。但我没有给它准确的索引,而是希望它找到那个单元格。 正如你在照片中看到的那样,我必须找到列A = traitement,B列= IDK和C列= BPL的行。

我查了一下,发现我必须使用Cells.Find,但我无法理解如何做到这一点!

1 个答案:

答案 0 :(得分:2)

这样的事情可以解决问题:

Dim v As Variant
Dim i As Long
Dim compteur As Long
Dim lineFound As Boolean

'Load range contents to array
v = Range("A1:C7").Value 'Or wherever your data is

'Iterate through array until desired content found
For i = 1 To UBound(v, 1)
    If v(i, 1) = "traitement" And v(i, 2) = "IDK" And v(i, 3) = "BPL" Then
        compteur = i
        lineFound = True
        Exit For
    End If
Next i

'Report
If lineFound Then
    MsgBox "The thing you want is on line " & compteur
Else
    MsgBox "Didn't find the thing you want."
End If