簡體   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