语法 - Range.SpecialCells方法VBA

时间:2017-01-22 11:28:02

标签: vba excel-vba range excel

通过使用Range.SpecialCells方法(Excel),是否可以选择包含文本" abc"在一个范围内?我不理解下面链接中的语法....

https://msdn.microsoft.com/en-us/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

由于

1 个答案:

答案 0 :(得分:0)

已修改以添加代码以存储已过滤的单元格而非删除它们

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行是标题

代码来&#34;存储&#34;具有给定文本标准的单元格为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