[英]Excel VBA Used Columns
我的Excel文件很大,我發現原因是在未使用的列中。 如果刪除它們,則文件大小正常。 未使用的列只是空白,或者至少看起來是空白。 但是,其中必須包含某些內容。 我如何找出什么以及如何將UsedRange重置為包含內容的實際列數?
我嘗試了以下方法:
myLastCol = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByColumns).Column
並且
myLastCol=ActiveSheet.UsedRange.Columns.Count
兩者都將返回值16383
。 有什么建議么? 這怎么發生(我確定它與我編寫的宏有關)?
您不能刪除未使用的列嗎? 如果是這樣,該解決方案如何?
Sub DeleteUnusedColumns()
Dim colRange As Range
Dim c As Long
'Define the target range
Set colRange = ActiveSheet.UsedRange
'Iterate through the columns
For c = colRange.Columns.Count To 1 Step -1
'If the column is empty then you can delete it
If Application.CountA(Columns(c).EntireColumn) = 0 Then
Columns(c).Delete
End If
'Increment the counter down
Next c
End Sub
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.