[英]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
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. 我可以根据需要重复多次,结果页面将自动清除旧的搜索值并仅列出新的搜索。
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.