简体   繁体   中英

Find cells within a range if one cell equals a criteria copy its value to another column

I'm having real trouble trying to get this to work for me. To help explain what I need to do I've produced a (hopefully) very simple example below...

What I would like to do is:

  • Within a range of cells A:A find J Bloggs (note there can be multiple entries and I need all of them)
  • When an order from J Blogs is found copy his order date B1 , request del. date C1 and actual del. date D1
  • Paste this information into a table G1:J4

NB: The list of customers can be long, and some customers may make seperate orders. I need to generate a list of all of these orders (don't need to check if date is in the past etc.).

Each time the query is run, say this time for H Simpson , only the details for H Simpson will appear in the table G1:J4

+---------------+----------------+---------------------+------------------+  
|   Customer    |   Order Date   | Requested Delivery  | Actual Delivery  |  
+---------------+----------------+---------------------+------------------+  
| J Bloggs      | 01/01/2013     |  02/01/2013         |  02/01/2013      |  
| H Simpson     | 05/01/2013     |  08/01/2013         |  09/01/2013      |  
| A Name        | 10/01/2013     |  10/01/2013         |  10/01/2013      |   
| J Bloggs      | 15/01/2013     |  22/01/2013         |  22/01/2013      |  
+---------------+----------------+---------------------+------------------+

There are at least three different approaches:

  1. Use AutoFilter
  2. Use a macro to extract the data
  3. Use VLOOKUP()

Using VLOOKUP() to get more than one result is explained here:

http://office.microsoft.com/en-us/excel-help/how-to-look-up-a-value-in-a-list-and-return-multiple-corresponding-values-HA001226038.aspx

Your lucky day! I had a free spare minutes and wrote this code for you.

It will ask you for the Name - you simply select the cell with the name you wan't to generate data for

It will create a table in columns G:J and stick in the results of matches in columns A:D

Sub Findining()

    Dim r As Range, i As Long, j As Long, rng As Range
    Range("G:J").ClearContents
    For i = 1 To 4
        Cells(1, i + 6) = Cells(1, i)
    Next i
    Set r = Application.InputBox("Select Name", Type:=8)
    If r.Columns.Count > 1 Or r.Rows.Count > 1 Then
      Do Until (r.Columns.Count = 1 And r.Rows.Count = 1)
        MsgBox "You can only select 1 name"
        Set r = Application.InputBox("Select Name", Type:=8)
      Loop
    End If

    For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
        Set rng = Range("A" & i)
        If StrComp(r, rng, vbTextCompare) = 0 Then
            For j = 0 To 3
                Cells(Cells(Rows.Count, rng.Offset(0, 6 + j).Column).End(xlUp).Row + 1, rng.Offset(0, 6 + j).Column).Value = rng.Offset(0, j).Value
            Next j
        End If
        Set rng = Nothing
    Next i
    Columns.AutoFit
End Sub

before:

在此处输入图片说明

after:

在此处输入图片说明

I go with Vasim every time, eg:

SO18655770示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM