VBA清除excel细胞

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 

如果有人能帮助我,这将是伟大的! 谢谢

首先select单元格然后使用Selection.ClearContents是一个坏主意(性能方面)

试试这个原则:

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

这将清除x,1和x + 29,1之间的所有单元格的内容

为了你的目的,这可能是这样的(你可能需要微调的细节,因为他们不清楚你的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 

我知道了。 谢谢@ 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