2016-03-15 99 views
1

数据布局:从A3开始到A(没有特定的最后一行)在名称管理器中称为= PeriodPrev。 = PeriodPrev是我用来标记数据的标签。 = PeriodCurr标签在PeriodPrev的最后一个已填充行之后开始。 PeriodPrev和PeriodCurr的其余数据位于列E到W.根据单元格范围清除行的内容

代码:如何在列A和列E中为属于= PeriodPrev的数据创建列A和列E中的数据清除内容? 我试过下面的代码,但它并没有完全达到上述目的。 “如果c.Value = ”PeriodPrev“,然后” 返回错误13. “如果c.Value =范围(” 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 

回答

0

使用相交

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

设我知道如果它不起作用

0

该代码有一些错误。我试图解决一些与注释有关的问题

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 
+0

我得到错误1004这一行: “LASTROW = .Range(PeriodPrev).Rows(.Range(PeriodPrev).Rows.count).Row” –

+0

我道歉。 PeriodPrev应该被引用。 – Jeeped

+0

嗨,Jeeped,它也行不通。我尝试了下面的代码,但在lastrow = ... etc这行仍然出现了相同的1004错误。 “子BYe2() 昏暗LASTROW只要 昏暗WS作为工作表 昏暗RNG作为范围 集WS = ThisWorkbook.Worksheets( “Data_LE”) 设置RNG = ThisWorkbook.Worksheets( “宏”)。范围(“PeriodPrev”) With ws lastRow = .Range(“PeriodPrev”).Rows(.Range(“PeriodPrev”).Rows.count).Row .Range(“A3:A”&lastRow&“, E3:W“&lastRow).ClearContents End With End Sub –

相关问题