我正在寻找一种方法来排除我的功能中的一些行。到目前为止,这是我的代码:
Dim W As Worksheet
Set W = ActiveSheet
Dim Last As Integer
Last = W.Range("Z2").End(xlToLeft).Column
If Last = 1 Then Exit Sub
Dim i As Integer
Dim j As Integer
For i = Last + 1 To 11
W.Cells(1, i).ClearContents
For j = 3 To 6
W.Cells(j, i).ClearContents
Next j
For j = 8 To 12
W.Cells(j, i).ClearContents
Next j
For j = 14 To 15
W.Cells(j, i).ClearContents
Next j
For j = 18 To 22
W.Cells(j, i).ClearContents
Next j
For j = 24 To 25
W.Cells(j, i).ClearContents
Next j
Next i
我正在尝试缩短代码并尝试使用数组但没有成功。任何帮助将不胜感激!
答案 0 :(得分:2)
尝试,
dim lc as long
with w
lc = .Range("Z2").End(xlToLeft).Column
if lc = 1 then exit sub
application.intersect(.range("1:1, 3:6, 8:12, 14:15, 18:22, 24:25"), _
.range(.columns(lc+1), .columns(11))).clearcontents
end with
答案 1 :(得分:1)
有两点可以使这更简单:
1)Range(Cell1, Cell2)
在两个角之间创建一个矩形。这样可以消除外环
2)有时从列表中排除行而不是包括每个区域更容易。有几种方法可以做到这一点,但是与InStr
结合使用的分隔文本字符串通常是一个基本选项。
Dim W As Worksheet
Set W = ActiveSheet
Dim Last As Integer
Last = W.Range("Z2").End(xlToLeft).Column
If Last = 1 Then Exit Sub
Dim i As Integer
For i = 1 to 25
If InStr("|2|7|13|16|17|23|", "|" & Cstr(i) & "|") = 0 Then
W.Range(W.Cells(i, Last+1), w.Cells(i, 11)).ClearContents
End If
Next i