我遇到了以下过滤器的问题。当自动过滤器没有返回任何内容(即没有结果)时,它似乎将所有内容复制到生成的新电子表格中。我怎样才能防止这种情况发生?我已经尝试事先插入一个检查,以查看过滤器后面是否有任何值,但它会不断返回一个非常大的数字(当它应该有效地返回2时,因为只有标题行A和B可见)。
With ThisWorkbook.Sheets("Master")
.AutoFilterMode = False
.Range("A2:Z2").AutoFilter Field:=refColumn, Criteria1:=itm
.Range("A2:Z2").AutoFilter Field:=26, Criteria1:="Chase them to activate their token"
' Check to see if there are any values after the filter
Dim FilterArea As Excel.Range
Dim RowsCount As Long
For Each FilterArea In ThisWorkbook.Sheets("Master").AutoFilter.Range.SpecialCells(xlCellTypeVisible)
RowsCount = RowsCount + FilterArea.Rows.Count
Next FilterArea
' If there are more than 2 rows then copy user id, first name last name to new spreadsheet
If RowsCount > 2 Then
.Range("A3:C" & LastRow).Copy
ActiveWorkbook.Sheets("Sheet1").Range("A11").PasteSpecial xlPasteValues
' Copy and paste email
.Range("E3:E" & LastRow).Copy
ActiveWorkbook.Sheets("Sheet1").Range("D11").PasteSpecial xlPasteValues
End If
End With
答案 0 :(得分:0)
在复制内容的代码中,您使用的是未定义的变量lastrow
。
对于您要实现的目标,查看AdvancedFilter
方法可能是有意义的。
答案 1 :(得分:0)
管理通过计算可见行数来计算出来:
With ThisWorkbook.Sheets("Master")
.AutoFilterMode = False
.Range("A2:Z2").AutoFilter Field:=refColumn, Criteria1:=itm
.Range("A2:Z2").AutoFilter Field:=26, Criteria1:="Verify if the user is happy with the service and that everything works ok"
' If there are more than 1 rows then copy user id, first name last name
Set VisibleRng = ThisWorkbook.Sheets("Master").AutoFilter.Range
RowCount = VisibleRng.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
If RowCount > 1 Then
.Range("A3:C" & LastRow).Copy
ActiveWorkbook.Sheets("Sheet1").Range("A" & NextHeaderRow + 4).PasteSpecial xlPasteValues
' Copy and paste email
.Range("E3:E" & LastRow).Copy
ActiveWorkbook.Sheets("Sheet1").Range("D" & NextHeaderRow + 4).PasteSpecial xlPasteValues
End If
End With