【问题标题】:Copy an entire row from sheet 2 which contains any value from column A on sheet 1, into sheet 3将工作表 2 中包含工作表 1 上 A 列中的任何值的整行复制到工作表 3
【发布时间】:2020-07-08 19:22:53
【问题描述】:

我希望能够从表 2 中复制包含表 1 中列 a 中的任何值的任何行。复制并粘贴到表 3 中。

我在网上找到了这段代码,但单元格值是特定的。我有大约 80 个值,因此单独列出它们需要很长时间。

Sub Test()
For Each Cell In Sheets(1).Range("J:J")
    If **Cell.Value = "131125"** Then
        matchRow = Cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy

        Sheets("Sheet2").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
    End If
Next
End Sub

【问题讨论】:

  • 如果您为要评估的范围分配一个变量:Dim myRange as Range, myCell as Range,那么您可以将 set myRange = Sheets("Sheet1").Range("A1:A15") 作为您要检查的单元格,然后再执行 For Each myCell in myRange @987654325 @ 最后以 Next myCell 结束
  • 或者将值存储在一个数组中,然后使用自动过滤器复制它们?
  • @Glitch_Doctor 感谢您的回复。我对excel编码一无所知,所以你能评论我应该输入的确切代码吗?谢谢!
  • 你能举个例子来说明第一个要粘贴的数组吗?

标签: excel vba excel-formula


【解决方案1】:

这个怎么样:

Option Explicit
Sub CopyThings()
    Dim rng As Range
    Dim rng1 As Range
    Dim ans As Integer
    On Error GoTo ISAIDRANGE
    Set rng = Application.InputBox("what do you want to copy?", "Select Range", Type:=8)
    ans = MsgBox("the whole row?", vbYesNo)
    Set rng1 = Application.InputBox("where do you want to paste", "Select Range", Type:=8)
    Application.ScreenUpdating = False
    rng1.Parent.Activate
    Select Case ans
        Case Is = vbYes
            rng.Rows.EntireRow.Copy rng1.Rows.EntireRow
        Case Is = vbNo
            rng.Copy rng1
    End Select
ISAIDRANGE:
    Application.ScreenUpdating = True
    If Err.Number = 424 Then ans = MsgBox("that's not a valid range", vbExclamation, "I meant a VALID range")
End Sub

【讨论】:

  • 现在,我不想这样做 80 次,但只要稍加努力,您就可以使用之前找到的代码片段来获取该代码片段,并为您的特定要求做出更有用的东西.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多