简体   繁体   English

SpecialCells(xlCellTypeVisible)

[英]SpecialCells(xlCellTypeVisible)

I have 15 columns of data, with rows ranging from 400 - 1000, and I have applied filters, I am keen to only copy visible cells, for column D and J, onto a different sheet, but paste special values through transpose into range D6. 我有15列数据,行的范围为400-1000,并且已应用过滤器,我希望仅将D和J列的可见单元格复制到另一张纸上,但是通过转置将特殊值粘贴到范围D6中。

I have used this below method, but it is only copying two visible rows, and not every single row according to the code, like it has done for me in the past for other sheets I have run after amending it. 我使用了下面的这种方法,但是它仅复制两个可见行,而不是根据代码复制每行,就像过去对我所做的修改后在其他工作表上所做的那样。 The problem could be I am running three or four process in one macro. 问题可能是我在一个宏中运行三个或四个进程。

I would be keen to know how I can amend this code so it copies column d and column j visible cells, excluding headers into a different sheet 我很想知道如何修改此代码,以便将d列和j列的可见单元格复制,但不将标题复制到另一张表中

So where do I stand with the code, it runs and applies the filters, but fails to copy all the rows for this particular part of the macro and secondly, I would be keen to know how to amend it so it only copies the Column D and J as the above excluding headers and only copies visible cells for to paste special values through transpose. 因此,我站在哪里看待代码,它运行并应用了过滤器,但是未能复制宏的这一特定部分的所有行,其次,我很想知道如何修改它,因此仅复制列D和J作为上述内容(不包括标题),仅复制可见的单元格以通过转置粘贴特殊值。

Set rngToCopy = .Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
Report.Range("D6").PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True


Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rngToCopy As Range, rRange As Range

    Set ws = Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        Set rRange = .Range("A1:A" & lRow)

        '~~> Remove any filters
        .AutoFilterMode = False

        With rRange 'Filter, offset(to exclude headers) and copy visible rows
            .AutoFilter Field:=1, Criteria1:="<>"
            Set rngToCopy = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
        End With

        '~~> Remove any filters
        .AutoFilterMode = False

        rngToCopy.Copy

        '
        '~~> Rest of the Code
        '
    End With
End Sub

I added thomas code to sub piece to see if the autofilter works and getting error 91 我在子片段中添加了托马斯代码,以查看自动过滤器是否正常工作并出现错误91

Sub Filter()
Dim Sheetx As Worksheet
Dim rngToCopy As Range, rRange As Range

With Sheetx

Set rRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))

With rRange

.AutoFilter Field:=11, Criteria1:="30"
.AutoFilter Field:=4, Criteria1:="1"
.AutoFilter Field:=2, Criteria1:="=*1", _
Operator:=xlAnd


With .SpecialCells(xlCellTypeVisible)

Set rngToCopy = Union(.Offset(0, 3), .Offset(0, 9))

End With

rngToCopy.Copy

End With
End With

End Sub

We can use Union and Range.Offset to join the cells together define the range. 我们可以使用UnionRange.Offset将单元格连接在一起来定义范围。

MSDN: Application.Union Method (Excel) MSDN:Application.Union方法(Excel)

Returns the union of two or more ranges. 返回两个或多个范围的并集。


Sub Sample()

    Dim lRow As Long
    Dim rngToCopy As Range, rRange As Range


    With Sheets("Sheet1")

            With .Range("A1").CurrentRegion
                .AutoFilter Field:=11, Criteria1:="=30"
                .AutoFilter Field:=4, Criteria1:="=1"
                .AutoFilter Field:=2, Criteria1:="=1", _
                Operator:=xlAnd

                On Error Resume Next
                Set rngToCopy = .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0

                If rngToCopy Is Nothing Then
                    MsgBox "SpecialCells: No Data", vbInformation, "Action Cancelled"
                    Exit Sub
                End If


                Set rngToCopy = Intersect(rngToCopy, .Range("B:B,H:H"))

                 If rngToCopy Is Nothing Then
                    MsgBox "Intersect: No Data", vbInformation, "Action Cancelled"
                    Exit Sub
                End If
        End With
    End With

    rngToCopy.Copy


    Sheets("Sheet2").Range("C6").PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM