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

huangapple go评论92阅读模式
英文:

how to count filtered columns in a table

问题

这段代码运行正常,但当筛选后的数据为空时,仍会复制标题行。如果将-1传递给它,它根本不会复制任何数据。你可以帮助我吗?

英文:

I'm trying to make this code works

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

It runs fine but when the filtered data shows nothing it's still copying the header and when I put a -1 it doesn't copy any data at all, can you help me out here?

I tried this code and runs but when the filter shows nothing on the rows still copies the header

答案1

得分: 2

' 在循环中重置对象变量
'
' 由于此代码位于循环中,您需要在属于“如果myRange为Nothing”部分的“End If”之前重置myRange,使用以下代码行“Set myRange = Nothing”。

For Each dest In destSS

    ' 做一些操作

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

    If myRange Is Nothing Then

        ' 什么都不做

    Else

        ' 做一些操作

        Set myRange = Nothing ' 就在这里!!!
    End If

Next dest
英文:

'Reset' Object Variable When In a Loop

  • Since this code is in a loop, you want to reset myRange i.e. right before the End If that belongs to If myRange Is Nothing, use the line Set myRange = Nothing.

<!-- language: lang-vb -->

For Each dest In destSS

    &#39; Whatever

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

    If myRange Is Nothing Then

        &#39; do nothing

    Else

        &#39; do stuff 

        Set myRange = Nothing &#39; right here!!!
    End If

Next dest

答案2

得分: 0

I'm sorry, but it seems that the provided code contains a mixture of code-related elements and text that should be translated. To assist you better, please provide the specific text that you would like to have translated.

英文:

Try the following code...

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

If Not myRange Is Nothing Then

    &#39;example to copy filtered data, including headers
    sheet8.ListObjects(&quot;BD&quot;).Range.Copy Sheet2.Range(&quot;a1&quot;)
    
    &#39;example to copy filtered data, excluding headers
    myRange.Copy Sheet3.Range(&quot;a1&quot;)
    
End If

huangapple
  • 本文由 发表于 2023年3月23日 08:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定