VBA,高级过滤器无法使用空白页

时间:2015-08-04 15:37:03

标签: excel vba excel-vba

我得到了以下宏,但是当我为一些文件运行它时只有标题而下面没有任何内容,我显然会收到错误,因为我将其更改为不包含标题。

是否有任何解决方法可能会填充“N / A”。而不是得到这个错误?我可以使用任何代码将N / A放在某些工作簿中空白的列中吗?或任何想法?

    Sub GetUniqueValues()




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.ActiveWorkbook.Worksheets("Unique data")
On Error GoTo 0

If wksSummary Is Nothing Then
    Set wksSummary = Excel.ActiveWorkbook.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
                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
            End If
        End If

    End With

Next wks


'Headers
Range("A1").Value = "File Name "
Range("B1").Value = "Sheet Name "
Range("C1").Value = "Column Name"

      Dim intRow As Long: intRow = 2

     For i = 1 To Sheets.Count
If Sheets(i).Name <> ActiveSheet.Name Then
    Cells(intRow, 2) = Sheets(i).Name
    Cells(intRow, 1) = ActiveWorkbook.Name
    intRow = intRow + 1
End If
   Next i




   End Sub

1 个答案:

答案 0 :(得分:1)

替换

wks.Range("C:C").AdvancedFilter xlFilterCopy, , r, True

使用:

If WorksheetFunction.CountA(wks.Range("C:C")) > 1 Then
    wks.Range("C:C").AdvancedFilter xlFilterCopy, , r, True
End If