简体   繁体   中英

Syntax - Range.SpecialCells Method VBA

By using the Range.SpecialCells Method (Excel), is it possible to select cells containing a text "abc" within a range ? I am not understanding the syntax... in the link below.

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")

not working

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

not working

rng.Rows.Delete Shift:=xlShiftUp

Thanks

edited to add the code to store filtered cells instead of deleting them

Specialcells() don't expose the behavior you're asking for

you'd better use AutoFilter()


code to delete cells with given text entire row

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

be sure row 1 is the header one


code to "store" cells with given text criteria into a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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