[英]vba clear column to last used row?
I am using vba to try and clear a column from row 10 to the last used row.我正在使用 vba 尝试清除从第 10 行到最后使用的行的列。
The problem i have is some of my values have gaps in them like so:我的问题是我的一些价值观存在差距,如下所示:
1
2
3
5
6
7
8
9
Here is my code:这是我的代码:
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
The problem i am getting is my code only clears upto the first blank row, and then doesn't clear anything after like so:我遇到的问题是我的代码只清除第一个空白行,然后像这样之后没有清除任何内容:
5
6
7
8
9
Please can someone show me the proper way of doing this?请有人告诉我这样做的正确方法吗?
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
it uses它用
Range(range1, range2)
notation Range(range1, range2)
表示法
to return a range
spanning from range1
to range2
ranges返回一个
range
从跨越range1
到range2
范围
Range("Col1:Col2")
notation Range("Col1:Col2")
表示法
to return a range
spanning between from columns Col1
to Col2
返回从列
Col1
到Col2
之间的range
UsedRange
property of Worksheet
object Worksheet
对象的UsedRange
属性
to return a rectangular range that embraces all actually "used" (ie, not empty or formatted) cells返回包含所有实际“使用”(即,非空或格式化)单元格的矩形范围
so:所以:
.Range(.Rows(10), .UsedRange.Rows(.UsedRange.Rows.Count))
gets all cells from row 10 to last "used" row获取从第 10 行到最后一个“已使用”行的所有单元格
.Range("H:M")
gets all cells in column H to M获取列 H 到 M 中的所有单元格
intersecting the two above ranges you get the wanted range to clear contents of将上述两个范围相交,您将获得所需范围以清除
This should work.这应该有效。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.