简体   繁体   English

VBA清除Excel单元格

[英]VBA to clear excel cells

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

I did the above coding to clear some boxes in excel, but it does not give me flexibility over range of boxes, what I want is to clear out any filled boxes in column A but if x mod 30 equals to zero to skip the next 3 and so on. 我做了上面的编码来清除excel中的某些框,但是它没有给我灵活性,超出了框的范围,我要清除的是A列中所有已填充的框,但是如果x mod 30等于0则跳过下一个3等等。 I have used a similar code to fill up the boxes, see below: 我使用了类似的代码来填充框,如下所示:

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

If anyone could help me that would be great! 如果有人可以帮助我,那就太好了! Thanks 谢谢

It's a bad idea (performance-wise) to first select the cell and then use Selection.ClearContents 首先选择单元格,然后使用Selection.ClearContents是一个坏主意(在性能方面)

Try this principle: 尝试以下原则:

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

This clears the contents of all Cells between x,1 and x+29,1 这将清除x,1和x + 29,1之间的所有单元格的内容

For your purpose it might be something like this (you might have to tweek the details because they are not clear from your post): 为了您的目的,可能是这样的(您可能不得不周详这些细节,因为您的帖子中不清楚这些细节):

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

I got it. 我知道了。 Thanks @E.Villager 谢谢@ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM