我正在使用下面的宏来保持单元格在指定的时间间隔内并删除其余的(即保留给定列中的第1,第5,第10等点)
Dim i As Long
Dim lastRow As Long
lastRow = Application.ActiveSheet.UsedRange.Rows.Count
For i = 2 To lastRow Step 5
Range(Rows(i), Rows(i+8)).ClearContents
Next i
目前,宏删除整个工作表上的整行。我想修改宏我可以选择我要修改的单个列顶部的单元格,并仅在该列上运行宏。
例如,我有数据,例如A1:B350和C1:E95(两者都在同一张纸上)。我希望能够运行宏并在A-B列中仅保留指定的单元格间隔,而不会干扰列C-E。同样,我想在C列中运行相同的宏而不会干扰A列中的数据。此时,我不知道如何修改此宏来完成此任务。我非常感谢任何帮助和指导。
答案 0 :(得分:0)
以下代码仅影响您选择的列,但我将步骤从8更改为12,否则所有值都被清除。另外,usedRange函数可能没有意义,因为现在只有一列是焦点。希望这段代码可以帮助您开始,您可以根据需要进行调整。
Sub delColumnData()
Dim r As Range, col As Long, LastRow As Long, i As Long
Set r = Application.InputBox("select column", , , Type:=8)
col = r.Column
Set r = Cells(1, col)
LastRow = r.End(xlDown).row
For i = 2 To LastRow Step 12
Range(Cells(i, col), Cells(i + 8, col)).ClearContents
Next i
End Sub
要处理多个列:
Sub delColumnsData()
Dim r As Range, col As Long, LastRow As Long, i As Long, j As Long
Set r = Application.InputBox("select column(s)", , , Type:=8)
For j = 1 To r.columns.Count
col = r(j).Column
LastRow = r(j).End(xlDown).row
For i = 2 To LastRow Step 12
Range(cells(i, col), cells(i + 8, col)).ClearContents
Next i
Next j
End Sub
答案 1 :(得分:0)
另一个选择
Option Explicit
Public Sub ClearColumnValues()
Dim i As Long, selectedCol As Long
selectedCol = Selection.Column 'in this case the Selection object can be convenient
With Application.ActiveSheet
For i = 1 To .UsedRange.Rows.Count Step 6
.Range(.Cells(i + 1, selectedCol), .Cells(i + 5, selectedCol)).ClearContents
Next
End With
End Sub