简体   繁体   English

如果一个单元格等于条件,则查找范围内的单元格将其值复制到另一列

[英]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) 在一个单元格区域A:A找到J Bloggs (请注意,可以有多个条目,而我需要所有这些条目)
  • When an order from J Blogs is found copy his order date B1 , request del. 当找到来自J Blogs的订单时,请复制其订单日期B1 ,请求del。 date C1 and actual del. 日期C1和实际del。 date D1 日期D1
  • Paste this information into a table G1:J4 将此信息粘贴到表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 每次运行查询时,例如这次对H Simpson ,表H Simpson G1:J4只会显示H Simpson的详细信息。

+---------------+----------------+---------------------+------------------+  
|   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() 使用VLOOKUP()

Using VLOOKUP() to get more than one result is explained here: 此处说明了如何使用VLOOKUP()获得多个结果:

http://office.microsoft.com/en-us/excel-help/how-to-look-up-a-value-in-a-list-and-return-multiple-corresponding-values-HA001226038.aspx 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 它将要求您提供Name -您只需选择名称不想要为其生成数据的单元格

It will create a table in columns G:J and stick in the results of matches in columns A:D 它将在G:J列中创建一个表格 ,并在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: 我每次都与Vasim一起去,例如:

SO18655770示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据一个条件查找范围,然后根据另一个条件查找该范围内的单元格 - How find a Range based on one criteria then a Cell within that Range based on another criteria 如果范围内只有一个单元格有值,如何将单元格范围内的值复制到另一个单元格? - How to copy the value in a range of cells to another cell if only one cell in the range has a value? 如果区域中只有一个单元格具有值,则将单元格区域中的值复制到另一个单元格 - Copy the value in a range of cells to another cell if only one cell in the range has a value 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 如果另一列等于值,将单元格从一列复制到剪贴板的宏 - Macro to Copy Cells from one Column to the Clipboard if Another Column Equals Value 根据另一列中的条件将一列中的单元格复制到另一张工作表 - Copy Cells in One Column, Based on Criteria in Another Column, to Another Sheet 将单元格从一张纸复制到另一张纸,对范围内的所有单元格重复 - Copy cell from one sheet to another, repeat for all cells within range 在单元格区域中单击任何单元格时,将其复制并粘贴为值 - Copy and Paste as Value when any cell is clicked within a range of cells 将单元格值复制到一系列单元格 - Copy cell value to a range of cells 1.使用单元格值查找范围内的值,2.复制并在右边添加3个单元格,然后3.将其粘贴到另一个范围内4.循环 - 1. Use cell value to find value in range, 2. copy it & 3 cells to the right then 3. paste it in another range 4. loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM