我想清除col A = "stack"
中指定列中的内容。我不想使用range("A2:A1000")
,因为这可能会改变。
这里有些麻烦,不确定我是否需要遍历每一行,谁能帮助我开始做这件事。
Sub Clear()
Dim stack As Range
stack= Sheets("database").Range("A2").Offset("stack", 0).endxlup.Select
Range("CJ:CW").stack.ClearContents
End Sub
答案 0 :(得分:2)
我将对您所描述的内容而不是代码进行讨论。
我想清除col A =的指定列中的内容 “堆”。我不想使用range(“ A2:A1000”),因为这可以更改。
我将其视为动态确定A列的最后一行,然后从A2
循环到它。如果给定单元格的A列值是“堆栈”,则清除同一行中CJ:CW
的内容。
Option Explicit
Public Sub ClearCells()
Dim mainRange As Range, rng As Range
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("database")
Set mainRange = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
For Each rng In mainRange.SpecialCells(xlCellTypeConstants)
If rng.Value = "stack" Then
rng.Offset(, 87).Resize(1, 14).ClearContents
End If
Next
End With
Application.ScreenUpdating = True
End Sub
阵列版本:
Option Explicit
Public Sub ClearCells()
Dim mainRange As Range, arr(), i As Long, j As Long
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("database")
Set mainRange = .Range("A2:CW" & .Cells(.Rows.Count, "A").End(xlUp).Row)
arr = mainRange.Value
For i = LBound(arr, 1) To UBound(arr, 1)
If arr(i, 1) = "stack" Then
For j = 1 To 14
arr(i, 87 + j) = vbNullString
Next
End If
Next
mainRange = arr
End With
Application.ScreenUpdating = True
End Sub