[英]vba clear column to last used row?
我正在使用 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)
表示法
返回一個range
從跨越range1
到range2
范圍
Range("Col1:Col2")
表示法
返回從列Col1
到Col2
之間的range
Worksheet
對象的UsedRange
屬性
返回包含所有實際“使用”(即,非空或格式化)單元格的矩形范圍
所以:
.Range(.Rows(10), .UsedRange.Rows(.UsedRange.Rows.Count))
獲取從第 10 行到最后一個“已使用”行的所有單元格
.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.