如何在一列中查找更多值

时间:2015-04-21 08:00:48

标签: excel vba find

我不知道如何使用“find”命令。

我需要在“M”列中找到单元格“F1”的值。如果该值在列中,则将值“M”列中右侧的值复制到“car”到“单元格”F2“。然后返回工作表1到单元格“G1”并重复直到单元格“cell,row = 1”为空。如果为空,则转到下一行到单元格“F2”...此循环执行,直到单元格“F = cell,row”为空。

见图: http://i57.tinypic.com/2hoydma.jpg

我有这段代码,但它不是循环代码:

Columns("M:M").Select
Selection.Find(What:=Range("F1"), After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(0, 1).Copy
Sheets("car").Select
Range("F2").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
Sheets("worksheet1").Select

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

我建议查看“for each”操作,这将迭代您的数据,直到列为空,下面的链接可能指向正确的方向

https://msdn.microsoft.com/en-us/library/office/aa221353%28v=office.11%29.aspx

答案 1 :(得分:0)

我试着解决这个问题。您可以修改它并且可以执行。

Dim myRng As Range
Dim nResult As Long
Set myRng = Worksheets("worksheet1").Range("M:M")
nResult = Application.CountIf(myRng, Range("F1"))

Set rFound = Worksheets("worksheet1").Cells.Find(What:=Range("F1"), _
             After:=Cells(Cells.Rows.Count, Cells.Columns.Count), _
             LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
             SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

For i = 0 To nResult - 1


            Worksheets("car").Range("F2") = rFound.Offset(0, 1).Value
            Set rFound = Worksheets("worksheet1").Cells.FindNext(rFound)


Next i

End

答案 2 :(得分:0)

试试这个,认为它应该做你想做的一切:

Dim TargetRange As Range
Set TargetRange = Sheets("worksheet1").Range("F1:K14")

Dim CurrentRange As Range

    For Each CurrentRange In TargetRange

        If CurrentRange.Value = "" Then
        Exit For
        End If

        Sheets("Car").Cells(CurrentRange.Row, CurrentRange.Column) = Application.WorksheetFunction.VLookup(CurrentRange.Value, Sheets("worksheet1").Range("M1:N7"), 1, False)

    Next CurrentRange