【发布时间】:2018-09-07 20:49:58
【问题描述】:
正如https://www.ozgrid.com/VBA/special-cells.htm作者所说:
当/如果一个人只指定一个单元格(通过选择或范围) Excel 将假定您希望使用整个工作表的单元格。
我的以下代码 (See the result) 确实选择了一个单元格,并且 .SpecialCells(xlConstants) 方法确实在整个工作表上运行,将所有单元格标记为恒定红色。但是,我的问题是,为什么 selection.Value = 1000 仅适用于单个选定的单元格(“A1”),而不是整个工作表(即所有单元格都填充 1000),根据应用于的逻辑.SpecialCells(xlConstants) 方法?
Sub stkOvflSep7()
' This sub marks red the cells with a constant
' The first cell is selected
' Some other cells are filled with constant
Dim constantCells As Range
Dim cell As Range
Worksheets("Sheet5").Cells.Clear
activesheet.Cells.Interior.Color = xlNone
Range("c1:d4").Value = 2
Range("a1").Select
ActiveCell.Select
selection.Value = 1000 ' The first cell is selected
' Set constantCells = Range("A1").SpecialCells(xlConstants)
Set constantCells = selection.SpecialCells(xlConstants)
For Each cell In constantCells
If cell.Value > 0 Then
cell.Interior.Color = vbRed ' marks red the cells with a constant
End If
Next cell
End Sub
【问题讨论】:
-
根据微软的说法; Range.SpecialCells 方法 (Excel)... 返回一个 Range 对象,该对象表示与指定类型和值匹配的所有单元格。
XlCellType指定单元格的类型。