簡體   English   中英

當范圍取決於變量時,如何刪除選定的單元格范圍?

[英]How to deleting selected range of cells when range is dependent on a variable?

我想刪除G123:P10000范圍內的單元格,但是G123123值不斷變化,因此如何在提供范圍的同時提供變量。

下面可以使用一次,但我想根據名為“ count”的變量的值多次使用它,該變量每次運行都會更改,因此只能從該范圍刪除

Range("G123:P10000").Select
Selection.ClearContents

我嘗試了以下類似的操作,但無法正常工作

count = ActiveSheet.Range("A1").End(xlDown).Row
Range("G$count:P10000").Select
Selection.ClearContents

您需要對.Offset().Resize()進行范圍修改。

...
count = CountRows(ActiveSheet.Range("A1"))
ActiveSheet.Range("G1").Offset(count,0).Resize(10000-count,1).ClearContents

Public Function CountRows(ByVal r As Range) As Long
    If IsEmpty(r) Then
        CountRows = 0
    ElseIf IsEmpty(r.Offset(1, 0)) Then
        CountRows = 1
    Else
        CountRows = r.Worksheet.Range(r, r.End(xlDown)).Rows.Count
    End If
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM