【问题标题】:Two sheets Filter data results only selective column copy paste to another sheet data should not replace while pasting两张表过滤数据结果仅选择性列复制粘贴到另一张表数据粘贴时不应替换
【发布时间】:2021-04-14 23:26:24
【问题描述】:

你能帮我解决这个小问题吗:让我解释一下我有一个包含多张工作表的工作簿

过滤苹果工作表A列结果一一复制几列粘贴到各个列中的相应给定工作表

同样需要为 Orange 结果一一复制几列粘贴到相应列中的相应给定工作表:这里它正在替换从 Apple 工作表复制的数据

您能否帮我粘贴数据时考虑该列的最后一行,我尝试了所有可能的方法仍然数据被替换

'Assign and set your variables

Sub data()

    'Application.ScreenUpdating = False
    'Application.CutCopyMode = True

    'Declare variable and give sheet names
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet, lRow As Long, lastrow As Long

    ' for example am showing only two sheet actualy i have lot of sheets here ( i dont know how it can be loop)
    Set ws1 = ThisWorkbook.Sheets("A")
    Set ws2 = ThisWorkbook.Sheets("B")
    Set ws3 = ThisWorkbook.Sheets("Apple") 
    Set ws4 = ThisWorkbook.Sheets("Orange")

    'Declare for last row
    Dim InputBox As String
    lRow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
    lastrow = ws3.Cells(ws3.Rows.Count, 10).End(xlUp).Row

    'Apple and orange sheet name header start from 4th row
    Sheets("Apple").Select
    Rows("4:36" & lRow).Clear

    Sheets("Orange").Select
    Rows("4:36" & lRow).Clear

    With ws1

        .Range("A1:Q1").AutoFilter Field:=1, Criteria1:="apple"
        .Range("J2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.Range("c4")
        .Range("P2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.Range("L4")
        .Range("Q2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.Range("K4")
        .Range("A1").AutoFilter 'clear the filter

        .Range("A1:Q1").AutoFilter Field:=1, Criteria1:="orange"
        .Range("J2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws4.Range("c4")
        .Range("P2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws4.Range("L4")
        .Range("Q2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws4.Range("K4")
        .Range("A1").AutoFilter 'clear the filter

    End With

'Below am not getting low row and while paste ( it is replacing old data)

    With ws2

        .Range("A1:S1").AutoFilter Field:=2, Criteria1:="apple"
        .Range("H2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.Range("K5" & lastrow)
        .Range("I2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.Range("L4")
        .Range("S2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.Range("C4")
        .Range("B1").AutoFilter 'clear the filter

        .Range("A1:S1").AutoFilter Field:=2, Criteria1:="Orange"
        .Range("H2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws4.Range("K4")
        .Range("I2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws4.Range("L4")
        .Range("S2").Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=ws4.Range("C4")
        .Range("B1").AutoFilter 'clear the filter

    End With
End Sub

【问题讨论】:

  • 您在粘贴来自ws1 的数据之前设置lastrow。另外ws3.Range("K5" & lastrow) 应该是ws3.Range("K" & lastrow)。其他列 L 和 C 相同。
  • 是的,我试过这个选项,它也会删除单元格并且没有结果....在同一张表中第二次粘贴数据时遇到问题...最后一行无法正常工作我很震惊你能帮忙吗我

标签: vba macos vba7 vba6


【解决方案1】:

在每次粘贴操作之前确定目标行。

Option Explicit

Sub data()

    Dim wb As Workbook, wsOut As Worksheet
    Dim n As Integer, lastRow As Long, targetRow As Long
    Dim arCrit, arOut, rng As Range

    Set wb = ThisWorkbook
    arCrit = Array("A", "B", "C", "D", "E")
    arOut = Array("A", "B", "C", "D", "E")

    'clear output sheets
    For n = 0 To UBound(arOut)
        Set wsOut = wb.Sheets(arOut(n))
        lastRow = wsOut.Cells(Rows.Count, "C").End(xlUp).Row
        If lastRow > 3 Then
            wsOut.Rows("4:" & lastRow).Clear
        End If
    Next

    With wb.Sheets("Apple") ' source
        
        ' filter/copy on each criteria A to Sheet A, B to Sheet B etc
        For n = 0 To UBound(arCrit)
            Set wsOut = wb.Sheets(arOut(n)) ' destination sheet A,B,C,D,E
            lastRow = .Cells(Rows.Count, 3).End(xlUp).Row
            .Range("A1:Q1").AutoFilter Field:=1, Criteria1:=arCrit(n)

            ' check for data
            Set rng = .Range("A1:Q" & lastRow).SpecialCells(xlCellTypeVisible)
            If rng.Rows.Count > 1 Or rng.Areas.Count > 1 Then
                targetRow = wsOut.Cells(wsOut.Rows.Count, "C").End(xlUp).Row + 1
                .Range("J2").Resize(lastRow - 1).SpecialCells(xlCellTypeVisible).Copy wsOut.Range("C" & targetRow)
                .Range("P2").Resize(lastRow - 1).SpecialCells(xlCellTypeVisible).Copy wsOut.Range("L" & targetRow)
                .Range("Q2").Resize(lastRow - 1).SpecialCells(xlCellTypeVisible).Copy wsOut.Range("K" & targetRow)
            End If
            .Range("A1").AutoFilter 'clear the filter
        Next
    End With

    With wb.Sheets("Orange") ' source

        ' filter/copy on each criteria A to Sheet A, B to Sheet B etc
        For n = 0 To UBound(arCrit)
            Set wsOut = wb.Sheets(arOut(n)) ' destination sheet
            lastRow = .Cells(Rows.Count, 3).End(xlUp).Row
            .Range("A1:S1").AutoFilter Field:=2, Criteria1:=arCrit(n)
          
            ' check for data
            Set rng = .Range("A1:S" & lastRow).SpecialCells(xlCellTypeVisible)
            If rng.Rows.Count > 1 Or rng.Areas.Count > 1 Then
                 targetRow = wsOut.Cells(wsOut.Rows.Count, "C").End(xlUp).Row + 1
                .Range("H2").Resize(lastRow - 1).SpecialCells(xlCellTypeVisible).Copy wsOut.Range("K" & targetRow)
                .Range("I2").Resize(lastRow - 1).SpecialCells(xlCellTypeVisible).Copy wsOut.Range("C" & targetRow)
                .Range("S2").Resize(lastRow - 1).SpecialCells(xlCellTypeVisible).Copy wsOut.Range("L" & targetRow)
             End If
             .Range("B1").AutoFilter 'clear the filter
        Next
    End With

    MsgBox "End"
End Sub

Edit1 - 交换输入/输出表

Edit2 - 在添加副本之前检查数据。苹果表过滤数据复制到表 A,橙色数据复制到表 B

Edit3 - 添加了工作表 C、D、E。标准和输出表相同。

【讨论】:

  • @basav 您的代码显示Destination:=ws3.Range("K5" & lastrow)Set ws3 = ThisWorkbook.Sheets("Apple") 。您想将 Apple、Orange 表中的数据复制到 A 和 B,对吗?
  • 让我们尝试一下会有帮助的示例... Apple 工作表 .... A 列过滤器找到“A”相关行...只有几列行需要粘贴到 A 工作表中特殊列... 下一部分... 橙片 .... B 列过滤器找到了“A”相关行... 只有几列行需要粘贴到最后一行的 A 表特殊 n 中(不应替换)....同样的苹果表过滤器A ...现在找到B行...复制几列并将B表中的相同粘贴到橙色表
  • 太棒了?完全按照要求工作......非常感谢
猜你喜欢
  • 2015-02-09
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 2019-10-22
  • 2020-01-13
  • 2011-07-21
  • 2020-06-12
相关资源
最近更新 更多