【发布时间】:2016-08-05 05:57:32
【问题描述】:
我正在尝试遍历“Sheet1”中的销售人员(A 列)和客户(B 和 C 列)列表。
我想通过“Sheet1”的A列,并根据销售人员,参考B和C列(在该销售人员的范围内),并将其与“Sheet2”中的A列进行比较。如果“Sheet1”的 B 或 C 列中的值与“Sheet2”的 A 列中的值匹配,我想复制整行,并将其粘贴到新的“Sheet3”中。
我一直在使用循环和条件,已经弄清楚如何根据另一张工作表中的条件复制和粘贴,但我正在努力解决基于“Sheet1”中的 A 列指定列 B 和 C 之间的链接,以及然后将它们与“Sheet2”的A列匹配。
我可以在一张纸上找到一些东西,然后复制并粘贴到另一张纸上,但这只是我想做的一小部分:
Sub CopyCode()
Dim r As Long, endrow As Long, pasterowindex As Long
endrow = Worksheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row
pasterowindex = 1
For r = 1 To endrow 'Loop through sheet1 and search for your criteria
'Central CODE:
If Worksheets("sheet2").Cells(r, Columns("A").Column).Value = "CLIENT" Then
'get all value(s) in range of column d (and c eventually)
' and see if they match values in column A of Readership paste
'if they do match values in column A of readership paste,
' then copy that matched row into a new sheet
' (will be designated by salesperson)
'Copy the current row
Rows(r).Select
Selection.Copy
'Switch to the sheet where you want to paste it & paste
Sheets("Sheet3").Select
Rows(pasterowindex).Select
ActiveSheet.Paste
'Next time you find a match, it will be pasted in a new row
pasterowindex = pasterowindex + 1
'Switch back to your table & continue to search for your criteria
Sheets("sheet2").Select
End If
Next r
End Sub
【问题讨论】:
-
显示您拥有的代码,它将帮助我们为您提供所需的指示。将代码放在原始帖子中,而不是 cmets 或作为答案。
-
您要复制到工作表 1 或工作表 2 上的工作表 3 的行?
-
我要复制/粘贴的行在第二张纸上。我想将它粘贴到第三张纸上。
标签: vba loops conditional match copy-paste