繁体   English   中英

语法-Range.SpecialCells方法VBA

[英]Syntax - Range.SpecialCells Method VBA

通过使用Range.SpecialCells方法(Excel),是否可以选择范围内包含文本“ abc”的单元格? 我不理解下面的链接中的语法。

https://msdn.microsoft.com/zh-CN/library/office/ff196157.aspx

sub sub1()

Dim rng As Range

Set rng = Worksheets("worksheet").Range("A1:A10").SpecialCells(xlCellTypeConstants, "abc")

不工作

Set rng = Worksheets("Output-Booth").Range("C18:C500").Find(What:="abc")

不工作

rng.Rows.Delete Shift:=xlShiftUp

谢谢

编辑添加代码以存储过滤的单元格,而不是删除它们

Specialcells()不会暴露您要求的行为

你最好使用AutoFilter()


代码以删除整行具有给定文本的单元格

Sub sub1()
    With Worksheets("worksheet").Range("A1:A10") '<--| reference your range
       .AutoFilter field:=1, Criteria1:="abc" '<--| filter referenced cells with "abc"
       If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete Shift:=xlShiftUp  '<--| if any filtered cells other than header then delete filtered cells skipping header
        .Parent.AutoFilterMode = False '<--| remove filtering
    End With
End Sub

确保第1行是标题1


代码将具有给定文本条件的单元格“存储”到一个range

Sub sub2()
    Dim rng As Range

    With Worksheets("worksheet").Range("A1:A10") '<--| reference your range
        .AutoFilter field:=1, Criteria1:="abc" '<--| filter referenced cells with "abc"
        If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then Set rng = .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible) '<--| if any filtered cells other than header then set 'rng' to filtered cells skipping header
         .Parent.AutoFilterMode = False '<--| remove filtering
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM