2017-03-07 160 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' :應用程序定義或對象定義的錯誤」 –