根据单元格范围清除行的内容

数据布局:从A3开始到A(没有特定的最后一行)在名称pipe理器中称为= PeriodPrev。 = PeriodPrev是我用来标记数据的标签。 = PeriodCurr标签在PeriodPrev的最后一个已填充行之后开始。 PeriodPrev和PeriodCurr的其余数据位于E列至W列。

代码:如何在列A和E到W中为列A中的数据属于= PeriodPrev创build明确的数据内容? 我已经尝试了下面的代码,但它并没有完全达到上述目的。 “如果c.Value =”PeriodPrev“,那么”返回错误13.“如果c.Value = Range(”PeriodPrev“)然后”返回错误1004。

Sub BYe() 'The following code is attached to the "Clear" button which deletes Previous Period data Dim c As Range Dim LastRow As Long Dim ws As Worksheet ws = ThisWorkbook.Worksheets("Sheet1") LastRow = Range("A" & Rows.count).End(xlUp).Row For Each c In Range("A3:A" & LastRow) If c.Value = "PeriodPrev" Then ' If c.Value = Range("PeriodPrev") Then c.EntireRow.ClearContents End If Next c End Sub 

使用相交

 If Not Application.Intersect(c, Range(yourLabel)) Is Nothing Then 

让我知道如果它不起作用

该代码有一些错误。 我试图解决一些与评论的问题

 Sub BYe() 'The following code is attached to the "Clear" button which deletes Previous Period data Dim c As Range, lastRow As Long, ws As Worksheet 'you need to SET a range or worksheet object Set ws = ThisWorkbook.Worksheets("Sheet1") 'you've Set ws, might as well use it With ws lastRow = .Range("A" & Rows.Count).End(xlUp).Row For Each c In .Range("A3:A" & lastRow) 'the Intersect determines if c is within PeriodPrev If Not Intersect(c, .Range("PeriodPrev")) Is Nothing Then 'this clears columns A and E:W on the same row as c. .Range(.Cells(c.Row, "A"), .Cells(c.Row, "E").Resize(1, 19)).ClearContents End If Next c End With End Sub 

以下应该执行没有循环的相同的动作。

 Sub BYe2() 'The following code is attached to the "Clear" button which deletes Previous Period data Dim lastRow As Long, ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") With ws lastRow = .Range("PeriodPrev").Rows(.Range("PeriodPrev").Rows.Count).Row .Range("A3:A" & lastRow & ",E3:W" & lastRow).ClearContents End With End Sub