跳到主要內容

如何在Excel中快速刪除多個空列?

有時,當您從某些來源(例如網頁,CSV,文本等)將數據導入Microsoft Excel時,可能會有許多空列。 刪除每個空列必須是耗時的。 因此,我們中的某些人尋求解決此問題的簡便方法。 本文收集了一些技巧性的技巧,以幫助您快速刪除多個空列。

使用公式刪除工作表中的多個空列

使用VBA代碼刪除工作表中的多個空列

刪除選區/活動工作表/整個工作簿中的多個空列,並提供有用的功能

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


使用公式刪除工作表中的多個空列

在Excel中,您可以應用公式來確定列是否為空白,然後使用 分類 功能可將所有空白列排序在一起,然後立即將其刪除。 請這樣做:

1。 在數據范圍的頂部添加新行,請參見屏幕截圖:

2。 然後將以下公式輸入到單元格A1中,然後將填充手柄拖動到右側以將此公式應用於其他單元格,如果該列為空白,則將顯示TRUE,否則顯示FALSE,請參見屏幕截圖:

=COUNTA(A2:A1048576)=0

3。 然後選擇包括助手行的數據范圍,然後單擊 數據 > 分類,請參見屏幕截圖:

4。 在彈出 分類 對話框,單擊 選項 按鈕,在下面 排序選項 對話框中選擇 從左到右排序 選項,請參見屏幕截圖:

5。 點擊 OK 按鈕返回 分類 對話框,然後選擇 行1 來自 排序 下拉菜單,然後選擇 單元格值 排序 部分中,選擇 最大到最小 來自 過往訂單 部分,請參見屏幕截圖:

6。 然後,所有空列都已排序在數據的左側,現在,您只需要選擇所有列,然後右鍵單擊,選擇 刪除 要立即刪除這些空白列,請參見屏幕截圖:


使用VBA代碼刪除工作表中的多個空列

如果您想使用VBA宏,事情會變得容易得多。 以下VBA代碼可能會對您有所幫助,請這樣做:

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

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

VBA代碼:刪除選擇中的多個空白列:

Sub DeleteEmptyColumns()'更新依據 Extendoffice
作為範圍的Dim rng作為範圍的Dim InputRng作為範圍xTitleId =“ KutoolsforExcel”設置InputRng = Application.Selection設置InputRng = Application.InputBox(“ Range:”,xTitleId,InputRng.Address,類型:= 8)Application.ScreenUpdating = False對於i = InputRng.Columns.Count到1步驟-1設置rng = InputRng.Cells(1,i).EntireColumn如果Application.WorksheetFunction.CountA(rng)= 0然後rng.Delete End如果下一個Application.ScreenUpdating = True End Sub

3。 按 F5 鍵以運行此宏,然後在彈出對話框中選擇所需的工作範圍。 看截圖:

4. 點擊 OK,然後刪除選擇中的所有空列。 看截圖:


刪除選區/活動工作表/整個工作簿中的多個空列,並提供有用的功能

刪除隱藏(可見)行和列 的效用 Excel的Kutools 可以幫助您一次快速刪除多個空行或列。

保養竅門:要應用此 刪除隱藏(可見)行和列 功能,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請這樣做:

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

2。 在 刪除隱藏(可見)行和列 對話框中,指定要從中應用操作的範圍 在看 下拉列表。 (如果您選擇 在選定範圍內,您必須先選擇要使用的範圍。)然後檢查 刪除類型。 並選擇 空白欄 詳細類型。 看截圖:

3。 然後點擊 OK。 並且空白列已從所選範圍中刪除。 看截圖:


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

有時,您可能希望刪除工作表中僅包含標題的所有空列,以下VBA代碼可以幫您一個忙,請執行以下步驟:

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

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

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

Sub deleteblankcolwithheader()
'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 column(s) with only a header row have 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鍵運行此代碼,將彈出提示框,提醒您標題為空白的列將被刪除,請參見屏幕截圖:

4.然後,單擊“確定”按鈕,一次刪除當前工作表中僅標題的所有空白列。 查看屏幕截圖:


更多相關文章:

  • 在Excel中刪除所有隱藏的行或列
  • 如果您有一個很大的工作簿,其中包含許多隱藏的行和列,而現在隱藏的數據不可用,那麼您想要刪除所有這些。 您如何快速刪除它們?
  • 在Excel中基於背景色刪除行
  • 如何根據背景色刪除整個行? 在此示例中,我需要刪除所有單元格填充有藍色背景色的所有行,如下面的屏幕截圖所示。 通過本文,您將獲得一些代碼以在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 (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
It deleted all my names in my first name column! Auuggh!
This comment was minimized by the moderator on the site
Thanks its very helpful page with basic excel option :)
This comment was minimized by the moderator on the site
Hi, can anyone guide how i can delete blank cells in a row or column...note whole blank column or rows... only blank cell i need to delete in a column
This comment was minimized by the moderator on the site
Hello,


try to implement below code
This comment was minimized by the moderator on the site
Hi, the thing which you are mentioned is so helpfull. I want a small help from your side. Daily am download the some files and using pivot prepared the reports. It is daily task for me and routine process. How can i do it with out using pivot. Ex: If i prepared the reports on yesterday in "A" Excel file. Next day in "A" Excel file just i replace the new "B" Excel file data. By this automatically report would be create? Is it possible?
This comment was minimized by the moderator on the site
Very helpful page. Thank you. :-) My challenge is that I have columns with headers, but no data in that column. Only the header is present. How would one go about deleting columns where there is no data, but the header is there? :o
This comment was minimized by the moderator on the site
the above details are very helpful for my personal use. Thanks for your guidance. M.KARTHIKEYAN :-)
This comment was minimized by the moderator on the site
I found it much simpler to go to the line after my last line of data, enter "Shift-Ctrl-End" to highlight all blank lines that followed(much faster), right click and enter "delete all rows/columns". The system didn't waste so much time searching this way and so it was clean in the blink of an eye and files that were 20 MB were dropped to a size of 20 kb.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations