跳到主要內容

如何在Excel中刪除帶有標題的空列?

作者:小陽 最後修改時間:2020-07-24

如果您有一個包含多個列的大型工作表,但是某些列僅包含一個標題,現在,您想要刪除這些僅包含標題的空列,以顯示以下屏幕截圖。 這可以在Excel中快速輕鬆地解決嗎?

doc刪除標題為1的空白列

使用VBA代碼刪除帶有標題的空列

使用Kutools for Excel刪除範圍/工作表/工作簿中的多個空列


箭頭藍色右氣泡 使用VBA代碼刪除帶有標題的空列

在Excel中,除了手動將它們逐個刪除外,沒有直接的方法來處理此作業,但是,在這裡,我可以為您介紹一個代碼,請執行以下操作:

1. 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:刪除帶有標題的空列:

Sub Macro1()
'updateby Extendoffice
    Dim xEndCol As Long
    Dim I As Long
    Dim xDel As Boolean
    On Error Resume Next
    xEndCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    If xEndCol = 0 Then
        MsgBox "There is no data on """ & ActiveSheet.Name & """ .", vbExclamation, "Kutools for Excel"
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For I = xEndCol To 1 Step -1
        If Application.WorksheetFunction.CountA(Columns(I)) <= 1 Then
            Columns(I).Delete
            xDel = True
        End If
    Next
    If xDel Then
        MsgBox "All blank and column(s) with only a header row have now been deleted.", vbInformation, "Kutools for Excel"
    Else
        MsgBox "There are no Columns to delete as each one has more data (rows) than just a header.", vbExclamation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = True
End Sub

3。 然後按 F5 鍵運行此代碼,然後會彈出一個提示框,提醒您帶有標題的空白列將被刪除,請參見屏幕截圖:

doc刪除標題為2的空白列

4。 然後點擊 OK 按鈕,將立即刪除當前工作表中僅有標題的所有空白列。

備註:如果有空白列,它們也會被刪除。


箭頭藍色右氣泡 使用Kutools for Excel刪除範圍/工作表/工作簿中的多個空列

有時,您只需要刪除空白列, Excel的Kutools刪除隱藏(可見)行和列 實用程序可以幫助您輕鬆完成此任務。

Excel的Kutools : 帶有300多個便捷的Excel加載項,可以在30天內免費試用. 

安裝後 Excel的Kutools,請執行以下操作:

1。 選擇包括需要刪除的空白列的列範圍。

2。 然後點擊 庫工具 > 刪除 > 刪除隱藏(可見)行和列,請參見屏幕截圖:

3。 在 刪除隱藏(可見)行和列 對話框中,您可以根據需要從“查找範圍”下拉列表中選擇刪除範圍,然後選擇 來自 刪除類型 部分,然後選擇 空白欄 來自 詳細類型 部分,請參見屏幕截圖:

doc刪除標題為4的空白列

4。 然後點擊 Ok 按鈕,則一次僅刪除空白列。 看截圖:

doc刪除標題為5的空白列

更多竅門:借助此強大功能,您還可以根據需要刪除空白行,可見列或行,隱藏列或行。

點擊免費下載Kutools for Excel現在!

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (10)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
All I keep getting is "There are no Columns to delete as each one has more data (rows) than just a header." Yet I know for a fact I have about 700 columns with a header and no data. I have a spreadsheet with over 900 columns and only 200ish have data below the header. Another macro worked but I had to delete the header row. So I know this macro is not working correctly. What's up?
This comment was minimized by the moderator on the site
Hello, Paul
The VBA code works well if there is only one header row in the worksheet, if there are two or more header rows, it will pop out the prompt message you said.
So you need to check out if there are hidden heading rows in your data.
Thank you!
This comment was minimized by the moderator on the site
Hello, I have the same issue, I know by fact that I have a lot of columns that have only headers but the same Error "There are no Columns to delete as each one has more data (rows) than just a header." and I have checked there are no hidden rows in the data.
Can anyone help me ?
This comment was minimized by the moderator on the site
Hi, Simoch
In this case, you can upload your Excel file attachment here, so that we can help to check the problem if you don't mind.
Thank you!
This comment was minimized by the moderator on the site
Dear skyyang no need, It worked perfecty now !! thank you so much for that VBA code !
Rated 5 out of 5
This comment was minimized by the moderator on the site
Amazing! You are correct. There must have been something hidden. Everything is working great now. Thank you so much for the quick reply.
This comment was minimized by the moderator on the site
Can this be saved in one spreadsheet and run on other files?
This comment was minimized by the moderator on the site
Works perfectly. Greatly appreciated
This comment was minimized by the moderator on the site
OMG, this is genius!!! thank you
This comment was minimized by the moderator on the site
Hi, Thanks for the nice code above. Is it free to use?
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations