简体   繁体   English

Excel VBA使用的列

[英]Excel VBA Used Columns

My Excel file is huge and I found out that the reason is in the unused columns. 我的Excel文件很大,我发现原因是在未使用的列中。 If I delete them the file has a normal size. 如果删除它们,则文件大小正常。 The unused columns are just blanks or at least appear to be blanks. 未使用的列只是空白,或者至少看起来是空白。 However, something must be contained in them. 但是,其中必须包含某些内容。 How can I find out what and how can I reset the UsedRange to the actual number of columns with content? 我如何找出什么以及如何将UsedRange重置为包含内容的实际列数?

I tried the following: 我尝试了以下方法:

 myLastCol = _
   .Cells.Find("*", after:=.Cells(1), _
    LookIn:=xlFormulas, lookat:=xlWhole, _
    searchdirection:=xlPrevious, _
    searchorder:=xlByColumns).Column

And also 并且

myLastCol=ActiveSheet.UsedRange.Columns.Count

Both would return the value 16383 . 两者都将返回值16383 Any suggestions? 有什么建议么? How can this happen (I am sure it has something to do with the Macro I wrote)? 这怎么发生(我确定它与我编写的宏有关)?

Can't you just delete the unused columns? 您不能删除未使用的列吗? If so what about this solution? 如果是这样,该解决方案如何?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM