vba清除列最后使用的行?

我正在使用vba尝试清除第10行到最后一个使用的行的列。

我所遇到的问题是我的一些价值观在他们之间存在差距:

1 2 3 5 6 7 8 9 

这是我的代码:

 Sub Clear2() ActiveSheet.EnableCalculation = False Application.ScreenUpdating = False Application.DisplayStatusBar = False Application.EnableEvents = False ActiveSheet.DisplayPageBreaks = False With Sheets(1) .Range("H10:H" & .Range("H10").End(xlDown).Row).ClearContents .Range("I10:I" & .Range("I10").End(xlDown).Row).ClearContents .Range("J10:J" & .Range("J10").End(xlDown).Row).ClearContents .Range("K10:K" & .Range("K10").End(xlDown).Row).ClearContents .Range("L10:L" & .Range("L10").End(xlDown).Row).ClearContents .Range("M10:M" & .Range("M10").End(xlDown).Row).ClearContents End With Application.ScreenUpdating = True Application.DisplayStatusBar = True Application.EnableEvents = True ActiveSheet.EnableCalculation = True End Sub 

我得到的问题是我的代码只清除到第一个空白行,然后不清除任何事后像这样:

 5 6 7 8 9 

请有人可以告诉我这样做的正确方法吗?

 With Sheets("mysheet") '<--| change "mysheet" to your actual sheet name Intersect(.Range(.Rows(10), .UsedRange.Rows(.UsedRange.Rows.Count)), .Range("H:M")).ClearContents End With 

它用

  • Range(range1, range2)表示法

    返回从range1range2范围的范围

  • Range("Col1:Col2")表示法

    返回从列Col1Col2之间的range

  • Worksheet对象的UsedRange属性

    返回包含所有实际“使用”(即非空格式或格式化)单元格的矩形范围

所以:

  • .Range(.Rows(10), .UsedRange.Rows(.UsedRange.Rows.Count))

    从第10行获取所有单元格到最后“used”行

  • .Range("H:M")

    将列H中的所有单元格都取为M

  • 相交两个以上的范围,你得到想要的范围来清除的内容

这应该工作。

 Option Explicit Sub Clear2() ActiveSheet.EnableCalculation = False Application.ScreenUpdating = False Application.DisplayStatusBar = False Application.EnableEvents = False ActiveSheet.DisplayPageBreaks = False With Sheets(1) .Range("H10:H" & .Cells(Rows.Count, "H").End(xlUp).Row).ClearContents .Range("I10:I" & .Cells(Rows.Count, "I").End(xlUp).Row).ClearContents .Range("J10:J" & .Cells(Rows.Count, "J").End(xlUp).Row).ClearContents .Range("K10:K" & .Cells(Rows.Count, "K").End(xlUp).Row).ClearContents .Range("L10:L" & .Cells(Rows.Count, "L").End(xlUp).Row).ClearContents .Range("M10:M" & .Cells(Rows.Count, "M").End(xlUp).Row).ClearContents End With Application.ScreenUpdating = True Application.DisplayStatusBar = True Application.EnableEvents = True ActiveSheet.EnableCalculation = True End Sub