跳到主要內容

如何在Excel中根據標題值刪除整個列?

在許多情況下,您可能需要根據Excel中的標題值刪除整個列。 例如,您需要刪除標題中包含值“ old”的所有列。 在本教程中,我們將詳細介紹根據標頭值刪除整個列的方法。

使用VBA代碼根據標題值刪除整個列
使用Kutools for Excel根據標題值刪除整個列


使用VBA代碼根據標題值刪除整個列

您可以使用VBA代碼根據標題值刪除整個列。 請執行以下操作。

1。 按 其他 + F11 同時打開 Microsoft Visual Basic應用程序 窗口。

2。 在裡面 Microsoft Visual Basic應用程序 窗口中,單擊 插入 > 模塊。 然後將下面的代碼複製並粘貼到“模塊”窗口中。

VBA代碼:根據標題值刪除整個列

Sub DeleteSpecifcColumn()
	Set MR = Range("A1:D1")
	For Each cell In MR
		If cell.Value = "old" Then cell.EntireColumn.Delete
	Next
End Sub

筆記:

1)在代碼中,“”是您要基於其刪除整個列的標題值。 請手動更改為標題值。 和 A1:D1 是您要使用的列範圍。
2) 此代碼區分大小寫。

3。 按 F5 鍵運行代碼,標題等於指定文本的整列將立即被刪除。


使用Kutools for Excel根據標題值刪除整個列

這款獨特的敏感免洗唇膜採用 Moisture WrapTM 技術和 Berry Mix ComplexTM 成分, Excel的Kutools選擇特定的單元格 實用程序,您可以輕鬆地首先選擇包含某些標頭值的所有列,然後立即刪除這些選定的列。 請執行以下操作。

申請前 Excel的Kutools首先下載並安裝.

1.選擇包含您要刪除的列的範圍,然後單擊 庫工具 > 選擇 > 選擇特定的單元格。 看截圖:

2。 在裡面 選擇特定的單元格 對話框中,選擇 整列 在選項 選擇類型 部分,並在 特定類型 下拉列表,選擇 等於 選項,然後在下面的框中鍵入標題值。 最後點擊 OK 按鈕。 看截圖:

3。 點擊 OK 在另一個彈出對話框中單擊,然後立即選擇包含指定標題值的列。

4.右鍵單擊所選列的列標題,然後單擊“確定”。 刪除 從右鍵單擊菜單中。 然後立即刪除所有選定的列。

  如果您想免費試用(30天)此實用程序, 請點擊下載,然後按照上述步驟進行操作。

最佳辦公生產力工具

🤖 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 (13)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, I need to delete a load of columns containing # in the headings - my understanding of VBA is very limited and the report has about 60 columns. The # is not always in the same location in each heading, so I need to identify any columns with the # in the heading and remove those columns. Is anyone able to guide me on this please?

I also need to split the report into 3 sections - the data runs across the worksheet (ie column A - H is Sales invoices; column I-Z is Purchase invoices and column AA-AG is Payments - each of these need to go onto a seperate worksheet - it is very labour intensive doing this manually so I am hoping someone can help me in VBA with this one as well.

Thanks
This comment was minimized by the moderator on the site
Hola, tengo una hoja de excel con varios datos, digamos 4 columnas y 12 filas, me gustaría poder imprimir cada fila en un folio separado en una plantilla, es decir, tenemos una hoja con varios nombres y apellidos y teléfono, quiero imprimir tantas páginas como filas tenga la hoja de excel, he inte tado varias cosas pero no encuentro la forma. Gracias de antemano
This comment was minimized by the moderator on the site
Hi,
If you want to print each row in a separate page in the worksheet, here is a suggestion that you insert page breaks every 1 row and then print them.
Methods provided in this article may do you a favor. Please have a try. Thank you.
How To Insert Page Break Every X Rows In Excel?[/url]
https://www.extendoffice.com/documents/excel/1774-excel-insert-page-break-every-row.html
This comment was minimized by the moderator on the site
The VBA example states that the code will delete all columns whose headers CONTAIN "old." That is not the case. The = operator will only delete exact matches. You need to use the LIKE operator with a wildcard.
Fixed code:Sub DeleteSpecifcColumn()
Set MR = Range("A1:D1")
For Each cell In MR
If cell.Value LIKE "old*" Then cell.EntireColumn.Delete
Next
End Sub

This comment was minimized by the moderator on the site
Hi,Thank you for your correction and sharing. 
This comment was minimized by the moderator on the site
Hi, is it possible to do this with multiple column names?
This comment was minimized by the moderator on the site
What if the headers are starting from 4th row
This comment was minimized by the moderator on the site
Hi Mark,
Try the below code. In the eighth line, please enclose each column name with double quotes and separate them by comma. Hope I can help. Thank you.

Sub DeleteSpecifcColumn()

Dim xFNum, xFFNum, xCount As Integer

Dim xStr As String

Dim xArrName As Variant

Dim MR, xRg As Range

On Error Resume Next

Set MR = Range("A1:N1")

xArrName = Array("old", "new", "get") 'enclose each column name with double quotes and separate them by comma

xCount = MR.Count

xStr = xArrName(xFNum)

For xFFNum = xCount To 1 Step -1

Set xRg = Cells(1, xFFNum)

For xFNum = 0 To UBound(xArrName)

xStr = xArrName(xFNum)

If xRg.Value = xStr Then xRg.EntireColumn.Delete

Next xFNum

Next

End Sub
This comment was minimized by the moderator on the site
I think line 12 needs to be changed to get the code to work. Original line 12 "Set xRg = Cells(1, xFFNum)" modified line 12 "Set xRg = MR(1, xFFNum)"
Sub DeleteSpecifcColumn()
Dim xFNum, xFFNum, xCount As Integer
Dim xStr As String
Dim xArrName As Variant
Dim MR, xRg As Range
On Error Resume Next
Set MR = Range("A1:N1")
xArrName = Array("old", "new", "get") 'enclose each column name with double quotes and separate them by comma
xCount = MR.Count
xStr = xArrName(xFNum)
For xFFNum = xCount To 1 Step -1
Set xRg = MR(1, xFFNum)
For xFNum = 0 To UBound(xArrName)
xStr = xArrName(xFNum)
If xRg.Value = xStr Then xRg.EntireColumn.Delete
Next xFNum
Next
End Sub
This comment was minimized by the moderator on the site
Hey! This works well until you have two contiguous columns with the same header. Say your range is A1:A5, and A2 and A3 have the header 'old'. It will delete A2, but then the A3 will move to the place A2 used to occupy, and the code will skip it, as it will be looking at the new A3, the next cell on its range.
This comment was minimized by the moderator on the site
Hi Elias,
As you mentioned, A2 and A3 have he same header "old". But they are in the same column, after applying the code, the whole column A will be removed immediately.
I am not sure I get your point. Would be nice if you could provide screenshot of what you are trying to do.
This comment was minimized by the moderator on the site
Sorry if this seems super basic but this is my first time trying vba. Using this how would I apply it to delete multiple column headers. I am actually trying to only keep certain header columns and delete the rest of a changing list.
This comment was minimized by the moderator on the site
In the first code just copy the 4th row and change the "old" into the second column name and it will work
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations