繁体   English   中英

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

[英]Clear Contents of Rows Depending on Cell Range

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

代码:如何为属于A列= PeriodPrev的数据在A和E到W列中创建清晰的数据内容? 我已经尝试了以下代码,但它并不完全符合上述目的。 “如果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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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