excel 如何计算表中筛选列的数量

2w2cym1i  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(134)

我在试着让这个代码工作

For Each dest In destSS

    Hoja8.ListObjects("BD").range.AutoFilter Field:=9, Criteria1:= _
        Array(dest), Operator:=xlFilterValues
        Hoja8.ListObjects("BD").range.AutoFilter Field:=10, Criteria1:= _
    "<=.8", Operator:=xlFilterValues
With Hoja8.ListObjects("BD").Sort
    .SortFields.Clear
    .SortFields.Add Key:=range("BD[Load Status]"), Order:=xlDescending
    .SortFields.Add Key:=range("BD[Destination]"), Order:=xlAscending
    .SortFields.Add Key:=range("BD[Current Location]"), Order:=xlAscending
    .SortFields.Add Key:=range("BD[Event]"), Order:=xlAscending
    .SortFields.Add Key:=range("BD[Dwell]"), Order:=xlDescending
    .Apply
End With
    
    On Error Resume Next
Set myRange = Hoja8.ListObjects("BD").DataBodyRange.CurrentRegion.SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If myRange Is Nothing Then

'Do nothing

Else
    'do stuff
    Sheets("BD").Activate
        range("A1").Select
        With Hoja8.ListObjects("BD")
          Union(.ListColumns(9).range, _
            .ListColumns(1).range, _
            .ListColumns(3).range, _
            .ListColumns(5).range, _
            .ListColumns(8).range, _
            .ListColumns(10).range, _
            .ListColumns(15).range, _
            .ListColumns(17).range, _
            .ListColumns(19).range).Select
    
    Selection.Copy
    Sheets("Recently Placed").Select
 'check ifs there any data in the sheet and if there are, find the last row to paste the data--------------
       
      If WorksheetFunction.CountA(range("C1").CurrentRegion) = 0 Then
        range("C1").Select
        ActiveSheet.Paste
        Else
        
        lscol = range("C" & rows.count).End(xlUp).Offset(1).Select
        
        ActiveSheet.Paste
        End If
        
    
End With
        Set myRange = Nothing
     End If
Next

        columns("G").Cut
        columns("C").Insert
 
Call SortDB


 Application.ScreenUpdating = True
End Sub

它运行正常,但当过滤后的数据显示什么也没有时,它仍然在复制头部,当我输入-1时,它根本不复制任何数据,你能帮我吗?
我尝试了这段代码,并运行,但当过滤器显示什么行仍然复制标题

9lowa7mx

9lowa7mx1#

在循环中“重置”对象变量

  • 由于这段代码是在循环中,你想重置myRange,也就是说,在属于If myRange Is NothingEnd If之前,使用Set myRange = Nothing行。
For Each dest In destSS

    ' Whatever

    On Error Resume Next
        Set myRange = sheet8.ListObjects("BD").DataBodyRange.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If myRange Is Nothing Then

        ' do nothing

    Else

        ' do stuff 

        Set myRange = Nothing ' right here!!!
    End If

Next dest
0pizxfdo

0pizxfdo2#

请尝试以下代码...

On Error Resume Next
With sheet8.ListObjects("BD").Range
    Set myRange = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
End With
On Error GoTo 0

If Not myRange Is Nothing Then

    'example to copy filtered data, including headers
    sheet8.ListObjects("BD").Range.Copy Sheet2.Range("a1")
    
    'example to copy filtered data, excluding headers
    myRange.Copy Sheet3.Range("a1")
    
End If

相关问题