【问题标题】:How do I choose certain values in one column (based on another) to match with a column in another sheet, for the purpose of copy-paste?为了复制粘贴,如何选择一列中的某些值(基于另一列)以匹配另一张表中的列?
【发布时间】: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


【解决方案1】:
for i = FirstRowA to LastRowA 'leaving you to figure out how to find these values
     If cell(i) <> vbNullString Then
          for h = FirstRowB to LastRowB 'again for you to figure out
               If cell(h) = cell(i) Then 
                    cell(h).copy
                    Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                    Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(0, 1).value = "Column b"
               End If
          Next h

          for j = FirstRowC to LastRowC 'again you figure this out
               If cell(j) = cell(i) Then 
                    cell(j).copy
                    Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                    Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(0, 1).value = "column c"
               end if
          Next j
      end if
next i

这里可能存在一些语法问题,但我基本上已经为您提供了您要求的循环。这只是让您可以轻松地自己解决剩下的问题。

【讨论】:

  • 非常感谢,这个很全面。您能否快速解释一下 vbnullstring 在这里代表什么?我将其解释为(参考我原来的问题)“销售人员”?
  • vbNullString 是 VBA 识别为 NULL 的内置函数/关键字。与 "" 相比,使用 vbNullString 来引用空白更有效。我只是养成了一种习惯,在可能的情况下总是使用它来处理空白。基本上,它表示如果循环中的当前单元格 cell(i) 不为空,则执行代码
  • @Wick 这能回答你的问题吗?如果是这样,请标记答案或要求更多:)
  • Doug - 仍在开发最终产品,但是是的,它确实帮助我朝着正确的方向前进!谢谢!
猜你喜欢
  • 2021-09-30
  • 2019-01-01
  • 1970-01-01
  • 2022-07-25
  • 2018-01-25
  • 1970-01-01
  • 2021-02-16
  • 2018-09-06
  • 1970-01-01
相关资源
最近更新 更多