首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

specialcells步骤研究不明白

2012-10-19 
specialcells方法研究不明白SpecialCells查找指定类型及值的单元格并返回单元格引用。它有两个参数,帮助中

specialcells方法研究不明白
SpecialCells查找指定类型及值的单元格并返回单元格引用。
它有两个参数,帮助中说,一个是类型,一个是值:
expression.SpecialCells(Type, Value)
Type
这里说类型,是指单元格中数据的表现形式。是常数,还是公式,或者是空单元格,或者是最后的单元格等等。它规定,可以查找下列10种类型中的任意一种,不同的类型用不同的参数,具体见帮助。
例如,在sheet1中查常数(省略第二个参数):
sheet1.cells.SpecialCells(xlCellTypeConstants),或者:
sheet1.cells.SpecialCells(2)
又如,在sheet2的A:D列查找包含公式的单元格:
sheet2.range("a:d").SpecialCells(xlCellTypeFormulas)
Value
如果查找常数或公式,可以使用第二个参数 Value。
可以指定单元格的值是“错误值、逻辑值、文本、数值”中的哪一种或几种,分别用数值16、4、2、1代表前面4种类型,指定的类型有多种时,值可以相加,如文本和数值:2+1=3:
sheet2.range("a:d").SpecialCells(xlCellTypeFormulas,3)
在指定区域中查找包含公式的单元格,而且,单元格计算结果为数值或文本。
第二个参数,如果不指定,默认为23,即全部。
第一个参数,如果是10个参数中的另外8个,则不使用第二个参数。

         Dim rng As Range        Dim i As Long        Dim theArea As Range '        Set rng = .Cells    .UsedRange.AutoFilter Field:=8, Criteria1:="#N/A"    .UsedRange.SpecialCells(xlCellTypeVisible).Copy (tempsheet1.Cells(1, 1))'        rng.AutoFilter Field:=8, Criteria1:="#N/A"''        rng.SpecialCells(xlCellTypeVisible).Copy (tempsheet1.Cells(1, 1))        Set rng = .UsedRange.SpecialCells(xlCellTypeVisible)        i = 0        Set theArea = .Cells        For Each theArea In rng.Areas        i = i + 1        'MsgBox theArea.row         'here the msgbox return the row of cells which selected        Next


Sub 数据筛选()    Dim rng As Range    Dim rngtemp As Range    Dim therng As Range    Dim srng As Range    Dim ss As Variant        Set rng = Range([a1], [d21])    rng.AutoFilter field:=1, Criteria1:="赵*", Operator:=xlOr, Criteria2:="王*" '可以用数组的形式,形如array("aaa","bb")     'rng.AutoFilter field:=2, Criteria1:="职工"        Set rngtemp = rng.SpecialCells(xlCellTypeVisible, 3)    For Each therng In rngtemp.Areas        If srng Is Nothing Then            Set srng = therng        Else            Set srng = Union(srng, therng)        End If    Next        MsgBox srng.Row    rng.AutoFilter           End Sub


不明白的是为什么,srng.row一直都是1,而不是其他的,也不能把specialcells的东西赋值给数组.

热点排行