与https://www.ozgrid.com/VBA/special-cells.htm一样,作者说:
如果只指定单个单元格(通过选择或范围),Excel将假定您希望处理整个单元格工作表。
下面的代码(见结果)确实选择了单个单元格,而.SpecialCells(xlConstants)方法确实对整个工作表进行操作,用常量红色标记所有单元格。然而,我的问题是,为什么selection.Value = 1000只在单个选定的单元格("A1")上工作,而不是根据应用于.SpecialCells(xlConstants)方法的逻辑,而在整个工作表(即所有单元格都填充了1000)上工作?
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发布于 2018-09-07 11:29:25
单元格是每个属性和方法的单元格(而不是整个工作表)。
你引用的特色菜..。
与https://www.ozgrid.com/VBA/special-cells.htm一样,作者说: 如果只指定单个单元格(通过选择或范围),Excel将假定您希望处理整个单元格工作表。
...is,因为在Excel中,您可以选择单个单元格或区域单元格,但不能取消选择所有单元格。由于这个原因--而且由于搜索和/或选择特定的单元格--单个单元格中的单元格不是很有用-- excel为这两个函数使用完整的工作表(我不完全确定是否有另一个函数),而只选择单个单元格(或引用为范围)。如果选择了多个单元格/引用excel,则使用这些单元格进行搜索。这同样适用于在工作表上手动运行搜索等。
发布于 2018-09-07 04:24:55
实际上,您并不是在做与链接文章相同的事情,因为您是在为变量赋值,而不是选择Range("A1").SpecialCells(xlConstants)。
不过,我怀疑使用范围版本会起作用。
https://stackoverflow.com/questions/52215151
复制相似问题