gpt4 book ai didi

excel - 如何清除除 X、Y 和 Z 列和 A、B、C 行之外的所有数据?

转载 作者:行者123 更新时间:2023-12-04 21:26:45 25 4
gpt4 key购买 nike

我看过this非常相似的问题,我认为用户@SQL Police 的回答很棒!但我不知道如何修改或调整它以产生我正在寻找的结果。这是我的(类似但不同的)问题:
我想删除不在第 24 - 26 列中的所有内容,但也不在第 1 - 6 行中。
我有代码可以单独执行,但不能一起执行。
删除指定列:

Sub ColClear()
Dim ws As Worksheet
Set ws = ActiveSheet

ws.Range(ws.Columns(1), ws.Columns(23)).ClearContents
ws.Range(ws.Columns(27), ws.Columns(ws.UsedRange.End(xlToRight).Column)).Clear

End Sub
删除指定行:
Sub RowClear()
With Sheets("SheetName")
.Rows(7 & ":" & .Rows.Count).ClearContents
End With
End Sub

But how can I do both at once? (The intersection of the two ranges, not the union)


谢谢!

最佳答案

适应你的要求,基本思路是得到双方的范围然后ClearContents分别地:

Option Explicit

Private Sub Test()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
Dim bottomLeftRng As Range
Set bottomLeftRng = .Range(.Cells(7, 1), .Cells(.Rows.Count, 23))

Dim bottomRightRng As Range
Set bottomRightRng = .Range(.Cells(7, 27), .Cells(.Rows.Count, .Columns.Count))
End With

bottomLeftRng.ClearContents
bottomRightRng.ClearContents
End Sub

关于excel - 如何清除除 X、Y 和 Z 列和 A、B、C 行之外的所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72639603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com