Range.SpecialCells 方法可以返回一個 Range 對象,該對象代表與指定類型和值匹配的所有單元格。 其語法如下: 表達式.SpecialCells(Type,Value) 其Type參數的可選XlCellType常量及對應值如下表:
| XlCellType 常量 |
值 |
說明 |
| xlCellTypeAllFormatConditions |
-4172 |
任意格式單元格 |
| xlCellTypeAllValidation |
-4174 |
含有驗證條件的單元格 |
| xlCellTypeBlanks |
4 |
空單元格 |
| xlCellTypeComments |
-4144 |
含有注釋的單元格 |
| xlCellTypeConstants |
2 |
含有常量的單元格 |
| xlCellTypeFormulas |
-4123 |
含有公式的單元格 |
| xlCellTypeLastCell |
11 |
已用區域中的最后一個單元格 |
| xlCellTypeSameFormatConditions |
-4173 |
含有相同格式的單元格 |
| xlCellTypeSameValidation |
-4175 |
含有相同驗證條件的單元格 |
| xlCellTypeVisible |
12 |
所有可見單元格 |
當Type參數為 xlCellTypeConstants 或 xlCellTypeFormulas時,Value參數有4種XlSpecialCellsValue 常量可選,見下表:
| XlSpecialCellsValue 常量 |
值 |
說明 |
| xlErrors |
16 |
錯誤值 |
| xlLogical |
4 |
邏輯值 |
| xlNumbers |
1 |
數字 |
| xlTextValues |
2 |
文本 |
如下SpecialCells(xlCellTypeConstants,23)語句中的23=16+4+1+2,即表示定位的常量包含4種類型的單元格:錯誤值+邏輯值+數字+文本
- Sub 定位常量()
- Range("a:a").SpecialCells(xlCellTypeConstants, 23).Select
- End Sub
如只要定位常量錯誤值及邏輯值(即16+4=20),則采用如下代碼:
- Sub test1()
- Range("a:a").SpecialCells(xlCellTypeConstants, 20).Select
