数据布局: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
答案 0 :(得分:0)
使用Intersect
If Not Application.Intersect(c, Range(yourLabel)) Is Nothing Then
如果它不起作用,请告诉我
答案 1 :(得分: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