简体   繁体   English

VBA删除多个工作表中的空白列。工作表计数是一个变量

[英]vba to delete blank columns in multiple worksheets.worksheet count is a variable

I am trying to delete all blank columns in multiple worksheets where the count of those worksheets are variable. 我试图删除多个工作表中的所有空白列,这些工作表的计数是可变的。

I have tried the following code and it works, however it gives me the error when it cannot locate the next sheet. 我试过下面的代码,它可以工作,但是当它找不到下一张纸时会给我错误。 I intend to share this and would like to be error free. 我打算分享这一点,并希望没有错误。

Sub delete_columns()
i = ThisWorkbook.Worksheets.count
a = 1
Do Until a = i
Dim MyRange As Range
Dim iCounter As Long
Set MyRange = ActiveSheet.UsedRange
For iCounter = MyRange.Columns.count To 1 Step -1
If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
Columns(iCounter).Delete 
End If
Next iCounter
i = i + 1
ActiveSheet.Next.Select
Loop
End Sub

The following should work: 以下应该工作:

Sub delete_columns()
    Dim ws As Worksheet
    Dim MyRange As Range
    Dim iCounter As Long
    'Loop through each worksheet - no need to Activate or Select each one
    For Each ws In ThisWorkbook.Worksheets
        Set MyRange = ws.UsedRange
        For iCounter = MyRange.Columns.count To 1 Step -1
            'Need to reference columns of MyRange, rather than columns of the
            'worksheet, because column 5 of MyRange might be column 7 of the
            'worksheet (if the first used cell was in column C)
            If Application.CountA(MyRange.Columns(iCounter).EntireColumn) = 0 Then
                MyRange.Columns(iCounter).Delete 
            End If
        Next iCounter
        'Or, if you want to delete empty columns which exist to the left
        'of the UsedRange, you could do the following
        'For iCounter = MyRange.Columns(MyRange.Columns.count).Column To 1 Step -1
        '    If Application.CountA(ws.Columns(iCounter).EntireColumn) = 0 Then
        '        ws.Columns(iCounter).Delete 
        '    End If
        'Next iCounter
    Next
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除VBA中工作表中提到的多个变量列 - Delete multiple variable columns mentioned in worksheet in VBA VBA 循环工作表以删除空白列 - VBA Looping through worksheets to delete blank columns Excel VBA:如果特定工作表上的特定列为空白,则删除空白列和整个行 - Excel VBA: Delete blank columns & entire rows if specific columns are blank on specific worksheet not working 使用 VBA 获取 Excel 中多个工作表中的行数 - 工作一次,更改为另一个工作表时失败 - Getting a count of rows in multiple Worksheets in Excel, using VBA - Works once, fails when changing to another worksheet 是否有 VBA 代码到 select 来自多个工作表的多个(和不同的总数)列复制并粘贴到新工作表中 - Is there a VBA code to select multiple (and varied totals of) columns from multiple worksheets to copy and paste into a new worksheet 将多个列从工作表复制到单个工作表 - Copying Multiple Columns from Worksheets to a single Worksheet 使用 Access VBA 删除多个工作表中的行和列 - Delete rows and columns in multiple worksheet using Access VBA VBA:使用嵌套的For Each将工作表作为变量遍历工作表 - VBA: looping through worksheets using nested For Each having worksheet as variable VBA 根据文件/工作表列表重命名多个目录中的多个工作表? - VBA to rename multiple worksheets in multiple directories based on file/worksheet list? 如何删除空白列VBA - How to delete blank columns VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM