![](/img/trans.png)
[英]How to copy a range within a row from one excel sheet to another based on a cell value
[英]Need to copy range of data from one excel sheet to another based on first coulmn value
我有一个excel表(下面带标题的示例示例),其中没有包含将近八千行的标题。 如果A列中的值匹配,我需要将B,C,D列中的值复制到另一张纸上。 同样的问题是,列A在每一行中都没有值。 仅当它将具有与前一行值不同的值时,才会填充它。
考虑下面的示例表:
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
选择1时的预期输出为:
ProdID Name Prop Reveiwer
1 abcName abcProp abcRev
qweName qweProp qweRev
asdName asdProp asdRev
eName eProp eRev
aName aProp aRev
我已经尝试了几种逻辑来使用VBA来实现上述输出,但是它们都不起作用。
任何人都可以帮助我获得可以得到预期输出的VBA代码。也请让我知道是否可以使用VBA以外的简单方法来实现。
最快的方法是填充第一列。 您应该在知道已完全填充的列(即第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
我已经在名为Sheet1
的电子表格中重新创建了您的数据
在Sheet2
我有一个仅包含标题的空白表
当我单击Sheet1
上的按钮时,系统会提示我InputBox
在此示例中,我将搜索产品ID值为1。以下是Sheet2
上的结果
我可以根据需要重复多次,结果页面将自动清除旧的搜索值并仅列出新的搜索。
将以下代码放在模块中。 如果您希望以与我相同的方式进行设置,则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.