简体   繁体   English

需要根据第一库仑值将数据范围从一个Excel工作表复制到另一个工作表

[英]Need to copy range of data from one excel sheet to another based on first coulmn value

I have excel table (sample example below with header) without headers with almost 8 thousand rows in it. 我有一个excel表(下面带标题的示例示例),其中没有包含将近八千行的标题。 I need to copy values of column B,C,D to another sheet if the value in column A matches. 如果A列中的值匹配,我需要将B,C,D列中的值复制到另一张纸上。 Also the problem is column A does not have value in each row. 同样的问题是,列A在每一行中都没有值。 It is populated only when it will have a value which is different from the previous row value. 仅当它将具有与前一行值不同的值时,才会填充它。

Consider sample table below: 考虑下面的示例表:

    ProdID    Name     Prop       Reveiwer  
    1        abcName  abcProp     abcRev  
             qweName  qweProp     qweRev  
             asdName  asdProp     asdRev  
    2        jhkName  jhkProp     jhkRev  
             mnbName  mnbProp     mnbRev  
    1        eName    eProp       eRev  
             aName    aProp       aRev  

Expected output when we choose 1 is : 选择1时的预期输出为:

    ProdID  Name     Prop     Reveiwer  
    1      abcName  abcProp   abcRev  
           qweName  qweProp   qweRev  
           asdName  asdProp   asdRev  
           eName    eProp     eRev  
           aName    aProp     aRev 

I have tried several logic to achieve above output using VBA but none of them worked. 我已经尝试了几种逻辑来使用VBA来实现上述输出,但是它们都不起作用。

Can anyone help me with VBA code which can get the expected output.Also let me know if this can be achieved with simple method other than VBA. 任何人都可以帮助我获得可以得到预期输出的VBA代码。也请让我知道是否可以使用VBA以外的简单方法来实现。

The fastest way is to populate the first column. 最快的方法是填充第一列。 You should have a cycle on a column you know it is fully populated (ie column 2) store the prodID in a variable and use it each time prodID is = "" for example: 您应该在知道已完全填充的列(即第2列)上有一个循环,例如,将prodID存储在变量中,并在每次prodID为=“”时使用它,例如:

i=start_row
While Range("B" & i) <> ""
 if Range("A" & i) <> "" then
  prod_id=Range("A" & i)
 else
  Range("A" & i)=prod_id
 end if
 i=i+1
wend

Search and List Cell Values Based On A Common ID 基于公共ID搜索和列出单元格值

I've recreated your data in a spreadsheet named Sheet1 我已经在名为Sheet1的电子表格中重新创建了您的数据

在此处输入图片说明

On Sheet2 I have a blank sheet with only the headers Sheet2我有一个仅包含标题的空白表

在此处输入图片说明

When I click the button on Sheet1 , I'll be prompted with an InputBox 当我单击Sheet1上的按钮时,系统会提示我InputBox

在此处输入图片说明

In this example, I'll search for Prod ID value of 1. The following is the result on Sheet2 在此示例中,我将搜索产品ID值为1。以下是Sheet2上的结果

在此处输入图片说明

I can repeat this as many times as I want, and the result page will automatically clear itself of old search values and only list new searches. 我可以根据需要重复多次,结果页面将自动清除旧的搜索值并仅列出新的搜索。


The Code 编码

Place the following code in a module. 将以下代码放在模块中。 FindAndShow is the macro you want to assign to the button shape on the first page, if you wish to set yourself up the same way I have it. 如果您希望以与我相同的方式进行设置,则FindAndShow是要分配给首页上的按钮形状的宏。

Sub FindAndShow()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1)
    Dim wsResult As Worksheet: Set wsResult = ThisWorkbook.Sheets(2)
    Dim prodID As String, prodRng As Range
    Dim myRowOffset As Long, mySearch As String, nextRow As Long

    'First is clearing old search items
    wsResult.Range("A2", "D" & wsResult.Cells(wsResult.Rows.Count, "B").End(xlDown).Row).Clear

    'Next we find the next blank row to start placing our results. As I have it, this will
    'always be 2 because we're clearing old data. I've left it dynamic to make modifying the
    'code easier.
    nextRow = wsResult.Range("B2", wsResult.Cells(wsResult.Rows.Count, "B").End(xlUp)).Row + 1

    'Here we take our input from the user.
    'You can change the prompt and title to fit your needs.
    prodID = InputBox("Which Production ID would you like to find?", "Production ID Search")
    Set prodRng = ws.Range("A:A").Find(What:=prodID, LookIn:=xlValues, LookAt:=xlWhole)

    'This is the loop that finds search items, and pastes them to the results page.
    'Normally having range.value = range.value would be quickest, but since we're dealing with
    'thousands of cells that are in sizable groups, the copy method will be most ideal.
    If Not prodRng Is Nothing Then
    wsResult.Range("A" & nextRow).Value = prodID
        firstResult = prodRng.Address
        Do
            myRowOffset = FindRowOffset(prodRng)
            ws.Range(prodRng.Offset(0, 1), prodRng.Offset(myRowOffset, 3)).Copy _
                wsResult.Range("B" & nextRow)
            Set prodRng = ws.Range("A:A").FindNext(prodRng)
            nextRow = nextRow + myRowOffset + 1
        Loop While Not prodRng Is Nothing And prodRng.Address <> firstResult
    End If
End Sub

Function FindRowOffset(myRange As Range) As Long
    'This functions only purpose is to see how far each search block goes.
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1)
    Dim i As Long: i = 1

    Do While myRange.Offset(i).Value = "" And myRange.Offset(i, 1) <> ""
        i = i + 1
    Loop
    FindRowOffset = i - 1
End Function

暂无
暂无

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

相关问题 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet Excel宏-根据值将单元格从一张纸复制到另一张纸 - Excel Macro - copy cells from one sheet into another based on value Excel 根据条件将数据从一张工作表复制到另一张工作表 - Excel copy data from one sheet to another based on a condition 根据单元格值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on cell value 将 excel 数据从一张纸复制到另一张纸 - Copy excel data from one sheet to another Excel基于一个列的值将整个行从一张纸复制到另一张纸 - Excel Copy whole row from a sheet to another sheet based on one column value Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match Excel宏复制单元格范围并将数据粘贴到另一个工作表 - Excel Macro Copy cell range & paste data one sheet to another excel vba 范围从一张纸复制到另一张纸(错误 1004) - excel vba range copy from one sheet to another (error 1004)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM