【问题标题】:Paste Range of filtered data in columns in the same order/format以相同的顺序/格式粘贴列中过滤数据的范围
【发布时间】:2019-05-30 08:59:52
【问题描述】:

我有下面的工作代码,当我从一列中选择数据时,它会将过滤后的数据复制到过滤后的单元格中。

当我尝试一系列多列时,它会将数据复制回单列并粘贴如下:column1V1、column1V2、column1V3 等

如何将过滤后的数据以相同的顺序/格式粘贴到其他列中?

Sub Filtered_Cells()

    Dim from As Range

    Set from = Application.InputBox("Select range to copy selected cells to", Type:=8)

    from.Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Call Copy_Filtered_Cells

End Sub

Sub Copy_Filtered_Cells()

    Set from = Selection
    Set too = Application.InputBox("Select range to copy selected cells to", Type:=8)

    For Each Cell In from
        Cell.Copy
        For Each thing In too
            If thing.EntireRow.RowHeight > 0 Then
                thing.PasteSpecial
                Set too = thing.Offset(1).Resize(too.Rows.Count)
                Exit For
            End If
        Next
    Next

End Sub

【问题讨论】:

  • 你能突出显示哪一行显示错误吗?
  • 嗨!不涉及任何错误。它工作正常。我想扩展它的功能,以便它可以从多个列复制多个数据并以相同的格式/顺序将其粘贴到其他列

标签: excel vba


【解决方案1】:

这对你有用吗?

Sub Copy_Filtered_Cells_New()

Dim from As Range, too As Range, fromRng As Range
Set from = Application.InputBox("Select range to copy cells from", Type:=8)
Set too = Application.InputBox("Select range to paste cells to", Type:=8)
Dim ws As Worksheet: Set ws = from.Worksheet
Dim arrRanges() As String: arrRanges = Split(from.SpecialCells(xlCellTypeVisible).address, ",")

Dim R As Long, X As Long, nextVisRow As Long

    For X = LBound(arrRanges) To UBound(arrRanges)  'For each visible range
        Set fromRng = ws.Range(arrRanges(X))
        With fromRng
            For R = 1 To .Rows.Count  'For each row in the selected range
                nextVisRow = NextVisibleRow(too.Cells(1, 1)) 'Get the next visible row for paste

                too.Offset(nextVisRow - too.row).Resize(1, .Columns.Count).Value = .Offset(R - 1).Resize(1, .Columns.Count).Value
                Set too = too.Offset(nextVisRow - too.row + 1)
            Next R
        End With
    Next X

End Sub

Function NextVisibleRow(rng As Range) As Long

Dim ws As Worksheet: Set ws = rng.Worksheet
Dim R As Long: R = rng.Cells(1, 1).row

    Do While True
        If Not ws.Rows(R).EntireRow.Hidden Then
            NextVisibleRow = R
            Exit Do
        End If
        R = R + 1
    Loop

End Function

【讨论】:

  • 这很棒,几乎可以按照执行的方式工作。它成功复制过滤后的数据,但是当粘贴发生时,它会进入过滤器
  • 这太完美了!!完全按照预期工作。谢谢!!
  • 有没有办法扩展它的“内存?”。例如,如果有很多数据,它会复制粘贴所有内容,直到某个点。
  • 我认为您想要实现的目标可以通过其他方式做得更好,我已重写代码以尽可能接近您的代码。不确定所选范围内的限制是多少……在您看来,lots of data 是多少?此外,您要从中复制的范围...处于同一级别(即:从“A1:A200”复制到“X1:X200”?为什么需要将其复制到隐藏行上?重点是,有更有效的方法来处理数据,如果处理得当,肯定会支持“大量数据”。
  • 它与工作相关,目标是减少复制和粘贴过滤单元格所浪费的时间,因为这项工作意味着我们经常使用过滤器。例如,大量数据可能意味着 2000 个单元格。范围并不总是在同一级别,但通常需要将单元格从可见范围复制到可见范围
【解决方案2】:

感谢用户 FAB,我能够进一步开发宏。现在它将任何可见单元格范围复制到任何可见数据,没有任何限制或问题。问题是数组无法“记录”超过 18 个左右的元素。我使用了将用户选择的数据复制到新工作表的技巧,这可以成功地归因于数组。 这是完成的代码。

Public copyRng As Range
Public wb As Workbook

Sub Copy_Paste_Filtered_Data()

Copy

Dim from As Range, too As Range, fromRng As Range
Set from = copyRng
Set too = Application.InputBox("Select range to paste cells to", Type:=8)
Dim ws As Worksheet: Set ws = from.Worksheet
Dim arrRanges() As String: arrRanges = Split(from.SpecialCells(xlCellTypeVisible).Address, ",")

Dim R As Long, X As Long, nextVisRow As Long

    For X = LBound(arrRanges) To UBound(arrRanges)  'For each visible range
        Set fromRng = ws.Range(arrRanges(X))
        With fromRng
            For R = 1 To .Rows.Count  'For each row in the selected range
                nextVisRow = NextVisibleRow(too.Cells(1, 1)) 'Get the next visible row for paste

                too.Offset(nextVisRow - too.Row).Resize(1, .Columns.Count).Value = .Offset(R - 1).Resize(1, .Columns.Count).Value
                Set too = too.Offset(nextVisRow - too.Row + 1)
            Next R
        End With
    Next X

wb.Activate
Application.DisplayAlerts = False
Sheets("Temp").Delete
Application.DisplayAlerts = True

End Sub

Function NextVisibleRow(rng As Range) As Long

Dim ws As Worksheet: Set ws = rng.Worksheet
Dim R As Long: R = rng.Cells(1, 1).Row

    Do While True
        If Not ws.Rows(R).EntireRow.Hidden Then
            NextVisibleRow = R
            Exit Do
        End If
        R = R + 1
    Loop

End Function

Public Function Copy()

Dim ws As Worksheet
Set wb = Workbooks("PERSONAL.XLSB")
Set copyRng = Application.InputBox("Select range to copy cells from", Type:=8)
copyRng.Select
Selection.Copy
    With wb
            Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
            ws.Name = "Temp"
    End With
wb.Activate
Range("A1").Select
ActiveSheet.Paste
Set copyRng = Selection

End Function

这使用“PERSONAL.XLSB”工作簿,因此请务必先在其中记录一个宏,以激活它,然后再使用此宏

【讨论】:

    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2013-09-28
    相关资源
    最近更新 更多