了解 Excel VBA 中“特殊单元格”的语法

Ser*_*lik 3 excel vba

我的代码的一部分如下所示:

With Selection
    Set ConstantCell = .SpecialCells(xlCellTypeConstants, xlNumber)
    Set FormulaCell = .SpecialCells(xltypeformulas, 21)
End With
Run Code Online (Sandbox Code Playgroud)

它是宏的一部分,用于为包含不同类型数据的单元格着色。我明白这部分的作用,但我想问的是这21代表什么?我发现在这个公式的语法中它代表了一些值,我认为它只会选择具有公式且等于 21 的单元格,但当我运行整个宏时情况并非如此。

ash*_*awg 5

好像:

Set FormulaCell = .SpecialCells(xlCellTypeFormulas, 21)
Run Code Online (Sandbox Code Playgroud)

是相同的:

Set FormulaCell = .SpecialCells(xlCellTypeFormulas, xlErrors + xlLogical + xlNumbers)
Run Code Online (Sandbox Code Playgroud)

因为:

如果TypexlCellTypeConstantsxlCellTypeFormulas,则此参数用于确定结果中包含哪些类型的单元格。这些值可以加在一起以返回多种类型。默认情况下选择所有常数或公式,无论其类型如何。

和:

XlSpecialCellsValue常量

xlErrors     +16

xlLogical     +4 

xlTextValues  +2

xlNumbers     +1 
Run Code Online (Sandbox Code Playgroud)

在:

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-specialcells-method-excel

(另外,xlCellTypeFormulas在您的示例中拼写错误。)