我想删除工作表中除指定范围之外的所有内容。范围取决于第2列中的数据行,“i”表示行数。
i = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
我想保留范围(“A1:A”& i)和(“B1:B”& i)
A行中的值可能超出范围。
这是我到目前为止的代码:
Sub Test()
Dim i As Long
i = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
Dim j As Long
j = i + 1
.Rows("j" & ":" & .Rows.Count).Delete
.Columns 'still determining how to delete columns 3 or greater
End Sub
答案 0 :(得分:1)
这是你需要的:
Sub Test()
Dim i As Long
With Sheet1
i = .Cells(.Rows.Count, 2).End(xlUp).Row
.Range(i + 1 & ":" & .Rows.Count).ClearContents
.Range("C1").Resize(.Rows.Count, .Columns.Count - 2).ClearContents
'or alternative way
'.Range(.Cells(1, 3), .Cells(.Rows.Count, .Columns.Count)).ClearContents
End With
End Sub