2013-08-27 13 views
0

我想查找具有特定值的Column L中的所有單元格,並返回與找到的那些單元格相同的行的Column D中的值。在一列中查找具有相同值的單元格,並返回來自同一行的單獨列的值

到目前爲止,我只能返回一個結果,這將是我列表中最重要的結果,但我還想找到所有其他結果,以及我不知道要使用的代碼。

只是爲了進一步解釋:單元格D11中的值是我想在表格「主列表」的列L中找到的值。假設我找到單元格L13,L15L20中的值,我想將單元格D13,D15D20中的值返回到ws的單元格「C37:C39」中。注意:不。有值可能不同,所以電池的返回值將剛剛從C37向下出現(像自動多重選擇,複製和粘貼)

這裏有一個小東西,開始撲騰:

Sub FindRelatedProducts() 
Dim cell As Excel.Range 
Dim D11Value As Variant 
Dim D11Row As Variant 
Dim ws As Worksheet: Set ws = Sheets("RShip") 

Set cell = ws.Range("D11") 
    D11Value = cell.Value 
    With Sheets("Master List") 
     D11Row = Application.Match(D11Value, .Range("L:L"), 0) 
     If Not IsError(D11Row) Then 
      ws.Range("C37") = .Range("D" & D11Row).Value 
     End If 
    End With 
End Sub 

回答

1

這裏是一個使用範圍變量的例子。

您需要爲輸入數據範圍和輸出數據的範圍定義範圍。然後在VBA中,您需要將wrk,inRngoutRng變量更改爲您定義的命名範圍,並更改forif塊中的列索引,以匹配您正在查找的數據的列索引。

Option Explicit 
Option Base 1 

Sub FindValues() 
    Dim wrk As Worksheet 
    Dim inRng As Range 
    Dim outRng As Range 

    Dim cntr As Long 
    Dim outCntr As Long 
    Dim findVal As Double 

    Set wrk = Worksheets("Data") 
    Set inRng = wrk.Range("LookupRange") 
    Set outRng = wrk.Range("OutputRange") 

    ' Clear the output range in case you have fewer values on this run than on the previous one 
    outRng.ClearContents 

    ' Set the value you are looking for 
    findVal = 1 

    ' Iterate through the rows in the input range. If you find the result you want then write it to the output range 
    For cntr = 1 To inRng.Rows.Count 
     If inRng(cntr, 1) = findVal Then ' Assumes the value you are finding is in column 1 of the input range 
      outRng(outCntr, 1) = inRng(cntr, 2) ' Assumes the values you are exporting is in column 2 of the input range 
      outCntr = outCntr + 1 
     End If 
    Next cntr 
End Sub 
相關問題