高级过滤器 - 排除标头

时间:2015-08-02 19:09:11

标签: excel vba

我有一个执行高级过滤器的宏。如何从中排除标题?我尝试将C:C更改为C2:C,但它无效。

Sub extractuniquevalues2()


Dim wks As Excel.Worksheet
Dim wksSummary As Excel.Worksheet
'----------------------------------------------------------------------------------
'edited so it shows in the 3rd column row +1.  Add the header and sheet name macro to this

On Error Resume Next
Set wksSummary = Excel.ThisWorkbook.Worksheets("Unique data")
On Error GoTo 0

If wksSummary Is Nothing Then
    Set wksSummary = Excel.ThisWorkbook.Worksheets.Add
    wksSummary.Name = "Unique data"
End If


'Iterate through all the worksheets, but skip [Summary] worksheet.
For Each wks In Excel.ActiveWorkbook.Worksheets

    With wksSummary

        If wks.Name <> .Name Then
            If Application.WorksheetFunction.CountA(wks.Range("C:C")) Then
                Call wks.Range("C:C").AdvancedFilter(xlFilterCopy, , .Cells(.Cells(.Rows.Count, 3).End(xlUp).Row + 1, 3), True)
            End If
        End If

    End With

Next wks


    End Sub

要向您展示视觉检查我的图片:[img] http://i.imgur.com/xGcAZMj.jpg[/img]

想要删除标题并逐行显示名称,而不包含空格。

修改 --------------------------------------- ---------------------------------

所以我这样做,得到错误:

    Sub testage()


Dim wks As Excel.Worksheet
Dim wksSummary As Excel.Worksheet
'----------------------------------------------------------------------------------
'edited so it shows in the 3rd column row +1.  Add the header and sheet name macro to this

On Error Resume Next
Set wksSummary = Excel.ThisWorkbook.Worksheets("Unique data")
On Error GoTo 0

If wksSummary Is Nothing Then
    Set wksSummary = Excel.ThisWorkbook.Worksheets.Add
    wksSummary.Name = "Unique data"
End If


'Iterate through all the worksheets, but skip [Summary] worksheet.
For Each wks In Excel.ActiveWorkbook.Worksheets



    Dim r As Range

    ' Get the first cell of our destination range...
       Set r = .Cells(.Cells(.Rows.Count, 3).End(xlUp).Row + 1, 3)

         ' Perform the unique copy...
           wks.Range("C:C").AdvancedFilter xlFilterCopy, , r, True

    'Remove the first cell at the destination range...
           r.Delete xlShiftUp



Next wks


   End Sub

1 个答案:

答案 0 :(得分:2)

不幸的是,没有。所有过滤功能都适用于标题,甚至是您复制到新目标的标题,例如您的情况。但您可以使用Delete xlShiftUp进行跟进,以删除目标范围内的第一个单元格并将所有内容移到某个位置:

Dim r As Range

' Get the first cell of our destination range...
Set r = .Cells(.Cells(.Rows.Count, 3).End(xlUp).Row + 1, 3)

' Perform the unique copy...
wks.Range("C:C").AdvancedFilter xlFilterCopy, , r, True

' Remove the first cell at the destination range...
r.Delete xlShiftUp