2017-03-07 114 views
0

我在Excel中导入数据在我的表中修复了95列我想创建一个宏或代码,只要我将数据粘贴到Excel中,如果整个列完全空白,那么它应该被删除。如何删除空白列VBA

enter image description here

回答

1

你可以试试这个:

Sub Main 
    Dim iCol As Long 

    With Worksheets("mySheetName").UsedRange '<--| change "mySheetName" to your actual sheet name 
     For iCol = .Columns.Count to 1 Step - 1 
      If WorksheetFunction.CountA(.Columns(iCol)) = 1 Then .Columns(iCol).EntireColumn.Delete 
     Next 
    End With 
End Sub 
+0

收到错误消息“需要的对象“ –

+0

什么行引发该错误? – user3598756

+0

请参阅编辑错字代码(WorsheetFunction - > WorksheetFunction)。让我知道。 – user3598756

0
Dim r As Range, rows As Long, i As Long 
Set r = ActiveSheet.Range(Cells(1, 1), Cells(lastrow, lastcolumn)) 
rows = r.rows.Count 
For i = rows To 1 Step (-1) 
If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).Delete 
Next 
+0

-getting错误 - “运行时error'1004' :应用程序定义或对象定义的错误” –