VBA清除excel细胞

时间:2017-11-09 09:14:44

标签: excel vba

Range("A4:A29").Select
Selection.ClearContents
Range("A34:A59").Select
Selection.ClearContents
Range("A64:A89").Select
Selection.ClearContents
Range("A94:A119").Select
Selection.ClearContents
Range("A124:A149").Select
Selection.ClearContents
Range("A154:A179").Select
Selection.ClearContents
Range("A184:A209").Select
Selection.ClearContents
Range("A1").Select

我做了上面的编码来清除excel中的一些框,但它不能给我灵活的框范围,我想要的是清除A列中任何填充的框但是如果x mod 30等于零则跳过接下来的3个等等。我使用类似的代码填写方框,见下文:

With RegExp
    .Pattern = "\bT[0-9A-Z\(\)\-]+"
    .Global = True
    .IgnoreCase = False
    Set matches = .Execute(txt)
End With                                        

For Each Match In matches
    If x Mod 30 = 0 Then
        x = x + 4
    End If
    Cells(x, 1) = Match
    x = x + 1
    Cells(x, 1) = Match
    If x Mod 30 <> 0 Then
        x = x + 1
    End If
Next

如果有人能帮助我那会很棒!谢谢

2 个答案:

答案 0 :(得分:0)

首先选择单元格然后使用Selection.ClearContents

是一个坏主意(性能方面)

尝试这个原则:

Range(Cells(x, 1), Cells(x + 29, 1)) = ""

这将清除x,1和x + 29,1

之间所有单元格的内容

出于您的目的,它可能是这样的(您可能需要调整细节,因为它们的帖子中不清楚):

For x = 0 to 9 ' repeat however many times you want
    startingRow = x * 33 + 1
    Range(Cells(startingRow, 1), Cells(startingRow + 28, 1)) = ""
Next

答案 1 :(得分:0)

我明白了。谢谢@ E.Villager

Private Sub CommandButton2_Click()
Dim lRow As Long
 Dim lCol As Long

 lRow = Cells(Rows.Count, 1).End(xlUp).Row

 For x = 4 To lRow

  If x Mod 30 = 0 Then
               x = x + 4
          End If
                Cells(x, 1) = ""

 Next

 End Sub