简体   繁体   English

使用AutoFilter的SpecialCells在VBA中获取可见单元格时出错

[英]Error when I use SpecialCells of AutoFilter to get visible cells in VBA

My main goal is to copy the visible cells of an autofilter and later copy the dimensions of the visible cells to the new sheet. 我的主要目标是复制自动过滤器的可见单元格,然后将可见单元格的尺寸复制到新工作表中。 I am using this code: 我正在使用此代码:

Sheets(1).AutoFilterMode = False
Sheets(1).Range("A1:A1").AutoFilter Field:=columnaNumeroIntervalo, criteria1:=CDec(paramCantidadCriterio)
Sheets(1).Range("A1:A1").AutoFilter Field:=columnaNumeroIntervaloUnidades, Criteria1:=paramUnidadesCriterio

MsgBox AutoFilter.Range.SpecialCells(xlCellTypeVisible)(2, 11).Value

With the last line I want to check the value oa cell. 在最后一行,我想检查单元格的值。 If I use Cells(2,11) instead of SpecialCells I can see that cells have all the cells of the sheet, visible and not visible. 如果我使用Cells(2,11)而不是SpecialCells我可以看到单元格包含了工作表的所有单元格,可见且不可见。 So I want to use SpecialCells . 所以我想使用SpecialCells

If I use Special cells I get the following error: 如果我使用特殊单元格,我会收到以下错误:

error '-2147417848 (80010108) in runtime. Automatization error.

For the time an the type of the execution, it seem to enter in a loop, and finally gives this error. 对于执行类型的时间,它似乎进入一个循环,最后给出了这个错误。 Perhaps SpecialCells modify the autofilter and then in each modification execute again the autofilter? 也许SpecialCells修改自动过滤器然后在每次修改中再次执行自动过滤器?

To work with the visible cells of an AutoFilter, you have to use Offset if you are planning to exclude Headers. 要使用AutoFilter的可见单元格,如果计划排除标题,则必须使用“ Offset The error you are getting is because you are missing a "." 你得到的错误是因为你错过了一个“。” before Cells(2,11) Cells(2,11)之前Cells(2,11)

'~~> Remove any filters
ActiveSheet.AutoFilterMode = False

'~~> Filter, 
With rRange 
  .AutoFilter Field:=1, Criteria1:=strCriteria

  '~~> offset(to exclude headers)
  Debug.Print .Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells(2,11).Value

  Debug.Print .SpecialCells(xlCellTypeVisible).Cells(2,11).Value
End With

'~~> Remove any filters
ActiveSheet.AutoFilterMode = False

I decided to add this as a part of this answer so that it might help someone else in the future. 我决定将此作为此答案的一部分添加,以便将来可能对其他人有所帮助。

Let's say our range is 比方说我们的范围是

A1:F6 A1:F6

在此输入图像描述

When you run the below code, depending on whether you are using Offset or not, you will get these results. 当您运行以下代码时,根据您是否使用Offset ,您将获得这些结果。

Option Explicit

Sub Sample()
    '~~> Remove any filters
    ActiveSheet.AutoFilterMode = False

    Dim rRange As Range
    Dim Rnge As Range

    Set rRange = Sheets("Sheet1").Range("A1:F6")

    '~~> Filter,
    With rRange
      .AutoFilter Field:=1, Criteria1:="<>2"

      '~~> Offset(to exclude headers)
      Set Rnge = .Offset(1, 0).SpecialCells(xlCellTypeVisible)

      Debug.Print Range(Rnge.Address).Address
      Debug.Print ActiveSheet.Cells(3, 2).Address
      Debug.Print Range(Rnge.Address).Cells(3, 2).Address

      Debug.Print "--------------------------------------------------"

      '~~> To include headers
      Set Rnge = .SpecialCells(xlCellTypeVisible)

      Debug.Print Range(Rnge.Address).Address
      Debug.Print ActiveSheet.Cells(3, 2).Address
      Debug.Print Range(Rnge.Address).Cells(3, 2).Address

    End With

    '~~> Remove any filters
    ActiveSheet.AutoFilterMode = False
End Sub

在此输入图像描述

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM