早上好..。
我试图在excel 2010中执行以下操作:
搜索“测试a”或“测试b”等Sheet1,如果找到“测试a”,则搜索工作表2,以A搜索“测试a”的所有出现,并将每一行的复制表2 ->P从工作表1中找到的初始“测试a”开始,到第2页中的每一行向下递增。
表1:
A组:
表2:
Col A,B,C,D,-> Col P
等
预期结果:
我从来没有做过任何excel/vb编码,我甚至很难开始!
我所能做的就是编写一个高级代码:
For search criteria 'test a|test b ..'
if sheet 1, col A equal to 'criteria' (save row where found)
if sheet2, col A equal to 'criteria'
copy sheet2, col b->col p, row (where 'criteria' found) > sheet1, Col B, row (where criteria found in sheet1), incrementing row downwards as we go.非常感谢关于如何完成这一点的一些指导!
非常感谢
发布于 2013-07-30 19:27:38
这是一个粗略的草图,应该可以让你开始。使用嵌套的循环结构,因此效率不高,但提供了基本功能,应该允许您自定义以更好地适应数据。
Sub SearchCriteria()
Dim ws1 As Worksheet
Set ws1 = Sheet1
Dim ws2 As Worksheet
Set ws2 = Sheet2
Dim ws1RowCounter As Integer
ws1RowCounter = 2
Dim ws2RowCounter As Integer
ws2RowCounter = 2
Dim innerCounter As Integer
innerCounter = 0
Dim ws1Select As String
Dim ws2Select As String
Dim copyRange As Range
Do While ws1.Cells(ws1RowCounter, 1) <> ""
'look for this value in ws2
ws1Select = ws1.Cells(ws1RowCounter, 1)
'loop through ws2 range, look for matches
Do While ws2.Cells(ws2RowCounter, 1) <> ""
ws2Select = ws2.Cells(ws2RowCounter, 1)
If InStr(ws1Select, ws2Select) > 0 Then
'copy range if match found
Set copyRange = Range(ws2.Cells(ws2RowCounter, 2), ws2.Cells(ws2RowCounter, 16))
copyRange.copy
Set copyRange = Range(ws1.Cells(ws1RowCounter + innerCounter, 2), _
ws1.Cells(ws1RowCounter + innerCounter, 16))
copyRange.PasteSpecial xlPasteAll
innerCounter = innerCounter + 1
End If
ws2RowCounter = ws2RowCounter + 1
Loop
ws1RowCounter = ws1RowCounter + 1
ws2RowCounter = 1
innerCounter = 0
Loop
End Subhttps://stackoverflow.com/questions/17943895
复制相似问题