vba - SpecialCells 根据范围大小返回不同的结果

标签 vba excel

我真的很困惑和好奇,为什么相同的编码,只是数据量不同,输出会完全不同?

With Sheets("control deck").Range("A2:A5000").SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=r[-1]C"
End With

With Sheets("control deck").Range("A2:A50000").SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=r[-1]C"
End With

第一个输出,使用上面单元格值的副本填充空白 第二个编码,用第一个单元格值的副本填充所有范围

我的数据:

>     1111    | abc    |x
>                      |y
>                      |z
>     
>     1112    | def    |R
>                      |S
>                      |T
>                      |U

我希望得到什么

1111    | abc    |x
1111      abc    |y
1111      abc    |z

1112    | def    |R
1112      def    |S
1112      def    |T
1112      def    |U

结果

    >     1111    | abc    |x
    >     1111      abc    |y
    >     1111      abc    |z
    >      
    >     1111    | abc    |R
    >     1111      abc    |S
    >     1111      abc    |T
    >     1111      abc    |U

有人知道为什么吗?

只有当范围超过 30000 `row

时才会发生这种情况

最佳答案

如果您使用的是 Excel 2007 或更早版本,SpecialCells 可以引用的不同单元格区域的数量有 8192 个限制

作为替代方案,请尝试此

Sub Demo()
    Dim r As Range
    Dim dat As Variant
    Dim i As Long

    Set r = Sheets("Sheet2").Range("A2:A50000")
    dat = r.FormulaR1C1
    For i = 1 To UBound(dat, 1)
        If dat(i, 1) = "" Then
            dat(i, 1) = "=r[-1]C"
        End If
    Next

    r = dat

End Sub

这也会快得多

关于vba - SpecialCells 根据范围大小返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740604/

相关文章:

excel - 在 VBA 中大写动态范围

excel - 查找单元格中的特定文本,如果它包含 VLOOKUP

excel - 使用 Excel VBA 将图片粘贴到 Outlook 邮件中的文本上

excel - 带转置的条件查找

excel - 显示消息框时继续工作

sql - 为什么我不能在 Excel 2010 中对 ODBCConnection 对象的 SQL ('[a-z]' 查询中使用字符范围 'LIKE' )?

vba - 如何格式化从特定行到下的 Excel 列

c# - ExcelDnaUtil 与 Interop.Excel

vba - 无法点击某些点来抓取信息

vba - 带有 VBA 的 Word 中的单个源文档