This will do it for you.
Sub clearRowsAtoH()
Dim i As Integer
For i = 1 To 8
Sheets("BAC").range(Sheets("BAC").Cells(4, i), Sheets("BAC").Cells(Rows.count, i).End(xlUp)).ClearContents
Sheets("JPM").range(Sheets("JPM").Cells(4, i), Sheets("JPM").Cells(Rows.count, i).End(xlUp)).ClearContents
Sheets("CITI").range(Sheets("CITI").Cells(4, i), Sheets("CITI").Cells(Rows.count, i).End(xlUp)).ClearContents
Next i
End Sub
EDIT: It can be much cleaner using ‘with’ statements.
Sub clearRowsAtoH()
Dim i As Integer
For i = 1 To 8
With Sheets("BAC")
.range(.Cells(4, i), .Cells(Rows.count, i).End(xlUp)).ClearContents
End With
With Sheets("JPM")
.range(.Cells(4, i), .Cells(Rows.count, i).End(xlUp)).ClearContents
End With
With Sheets("CITI")
.range(.Cells(4, i), .Cells(Rows.count, i).End(xlUp)).ClearContents
End With
Next i
End Sub
VBA Delete multiple Columns Excel Macro Example Code
VBA code to Delete multiple Columns example will help us to delete multiple Columns from excel worksheet. We can use Delete method of Columns to delete the multiple Columns. In this example we will see how to delete the multiple Columns in excel worksheet using VBA. VBA code for deleting multiple Columns macro should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.
VBA code to delete multiple Columns
Here is the Example VBA syntax and Example VBA Macro to delete multiple Columns from excel worksheets. This will help you to know how to delete specific and multiple Columns from Excel workbook using VBA.
VBA Delete multiple Columns: Syntax
Following is the VBA Syntax and sample VBA code to delete multiple Columns from worksheet using VBA. We are using the Delete method of the Columns object of worksheet.
Columns("[Column Names]").EntireColumn.Delete
Here Column Names are your Column Names to delete. And EntireColumn.Delete method will delete the Entire Columns from the Excel spreadsheet.
Delete multiple Columns using VBA: Examples
The following VBA code is to delete multiple Columns from the worksheet. This code will delete the multiple Columns (A to C) which we have mentioned in the code.
Sub sbVBS_To_Delete_Multiple_Columns () Columns("A:C").EntireColumn.Delete End Sub
Instructions to run the VBA code to delete multiple Columns
Please follow the below steps to execute the VBA code to delete multiple Columns from worksheets.
Step 1: Open any existing Excel workbook
Step 2: Press Alt+F11 – This will open the VBA Editor
Step 3: Insert a code module from then insert menu
Step 4: Copy the above code and paste in the code module which have inserted in the above step
Step 5: enter some sample data in Column A to E
Step 6: Now press F5 to execute the code
Now you can observe that the entire Columns from A to C are deleted from worksheet.
Explained VBA Code to Delete Multiple Columns:
Starting program and sub procedure to write VBA code to delete entire Column from sheet.
Sub sbVBS_To_Delete_Multiple_Columns_C()
‘Specifying the Columns to delete and Deleting the Columns using EntireColumn.Delete method.
Columns(“A:C”).EntireColumn.Delete
End Sub
Ending the sub procedure to delete entire Column.
Here Columns(“A:C”) is to tell excel to delete Columns from A to C of the worksheet. And Delete method will delete the all specified Columns form the worksheet.
Delete Specific Multiple Columns using VBA: Examples
Here is the following VBA code is to delete specific multiple Columns from the worksheet. This code will delete the multiple specific Columns (A, C, H, K to O and Q to U) which we have mentioned in the code.
Sub sbVBS_To_Delete_Specific_Multiple_Columns() Sheets("Sheet1").Range("A:A,C:C,H:H,K:O,Q:U").EntireColumn.Delete End Sub
The above example delete the columns A, C, H, K to O and Q to U from the sheet named ‘Sheet1’.
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
Save Up to 85% LIMITED TIME OFFER
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates
Excel Pack
50+ Excel PM Templates
PowerPoint Pack
50+ Excel PM Templates
MS Word Pack
25+ Word PM Templates
Ultimate Project Management Template
Ultimate Resource Management Template
Project Portfolio Management Templates
Related Posts
- VBA code to delete multiple Columns
- VBA Delete multiple Columns: Syntax
- Delete multiple Columns using VBA: Examples
- Instructions to run the VBA code to delete multiple Columns
- Explained VBA Code to Delete Multiple Columns:
- Delete Specific Multiple Columns using VBA: Examples
VBA Reference
Effortlessly
Manage Your Projects
120+ Project Management Templates
Seamlessly manage your projects with our powerful & multi-purpose templates for project management.
120+ PM Templates Includes:
19 Comments
-
Shahid
January 12, 2015 at 9:50 PM — ReplyI tried this code, but it gives me an syntax error
I do have data available from A to ZCan someone assist
-
PNRao
January 13, 2015 at 4:15 PM — ReplyHi Sahid,
This code should work, please provide the code which you are testing:
Columns("A:Z").EntireColumn.Delete ' this will delete the Columns A to Z 'Or if you only want to clear the data Columns("A:Z").Clear ' this will clear the Columns A to Z
Thanks-PNRao!
-
Samantha
August 1, 2015 at 1:02 AM — ReplyHi, I need to delete random columns (ex. A,C,G,H). I tried doing it like you explained with the multiple random rows but it didn’t work
Thanks!
-
PNRao
August 2, 2015 at 3:38 AM — ReplyHi Samantha,
You can use the below procedure to delete specific columns:
Sub sbDeleteColumns() arrCol = Array("A", "C", "G", "H") 'always in ascending order For i = UBound(arrCol, 1) To 0 Step -1 Columns(arrCol(i)).EntireColumn.Delete Next End Sub
Please note: Always store all the Column names in an array variable(arrCol) in ascending order.
Thanks-PNRao!
-
Clark
August 14, 2015 at 2:55 AM — ReplyHow can I delete multiple columns for columns A, C, H, K-O, Q-U. Column K-O means column K to O. Thank you.
-
PNRao
August 14, 2015 at 3:21 PM — ReplyHi Clark,
I have updated the post with the example. Please check the last example to delete multiple specific columns from the excel workbook sheet.
Thanks-Valli!
-
chitra
September 28, 2015 at 10:08 AM — ReplyHi Friend,
i have data of around 900 columns and i need to delete every 3 columns after first column, how do i do this…. eg: i have to leave column 1 and delete 2,3 &4 and leave column 5 and delete 6, 7, & 8 and leave 9 and so on….. help me….
-
PNRao
September 29, 2015 at 12:01 AM — ReplyHi Chitra,
Please find the below code to delete columns as per above mentioned criteria.
Sub Delete_Columns() Dim StCol As Integer, EnCol As Integer For iCntr = 1 To 900 StCol = find_Column_Number(iCntr + 1) 'Start Column EnCol = find_Column_Number(iCntr + 3) 'End Column Sheets("Sheet1").Columns(StCol & ":" & EnCol).Delete Next End Sub
‘Function to find out column name from column number
‘For more detail: http://analysistabs.com/excel-vba/column-number-name/Function find_Column_Number(ByVal ColumnNumber As Integer)
find_Column_Number = Replace(Replace(Cells(1, ColumnNumber).Address, “1”, “), “$”, “)
End FunctionRegards- Valli
-
Praveen
November 1, 2015 at 11:37 PM — ReplyWill you please help me with VBA code..
R Name Dname Rcount Dcount
Ramesh Arun
Ramesh Arun
Ramesh Ganesh 3 2(delete other duplicates keep 1st time appeared ) how to do it by vba..? -
Joe
November 22, 2015 at 6:36 PM — Replythanks for the macros — all very helpful.
Joe
-
Syrine
December 7, 2015 at 2:03 AM — ReplyHi,
thank you for the code. Actually i want to execute the same code in my excel sheet because I need to do the same thing but it didn’t work. Would you help me find out what is wrong? I always receive the same error message “sub or function not defined”.
Many thanks
Regards
Syrine
-
Mike
January 4, 2016 at 9:17 PM — ReplyTrying to use this for deleting multiple columns:
Sub DelCols()
Range(“A:D,H:L,O:X,AA:BG”).EntireColumn.Delete
End Sub
Problem is I keep getting an exception on the colon between A & D. expected list seperator or )
I don’t see the problem, but maybe I’ve just been staring at it too longThanks
-
PNRao
January 4, 2016 at 10:11 PM — ReplyHi Mike,
I have tried your code and its working for me.'your code Sub DelCols() Range("A:D,H:L,O:X,AA:BG").EntireColumn.Delete End Sub
Try this alternative code, this will use the Columns object:
Sub deleteColumns() Dim strColumns As String Dim arrColumns,i strColumns = "A:D,H:L,O:X,AA:BG" 'Define your columns here arrColumns = Split(strColumns, ",") For i = 0 To UBound(arrColumns, 1) Columns(arrColumns(i)).EntireColumn.Delete Next i End Sub
Thanks-PNRao!
-
Aaron
January 28, 2016 at 8:54 PM — ReplyHello,
I am creating a template to be used by numerous people for various projects which, depending on the project, a different number of columns will be required.
To help explain, Columns A&B are fixed and I do not want any to be deleted. Columns C-Q have data for them for each country that is within a project. I currently have 15 columns here as this is the max number of countries possible. If someone uses data for a project with only 3 countries, columns C, D and E would display the country name as the header and relevant data underneath, and columns F – Q will just be blank (albeit with formulae within the cell just returning a blank result). I want to use a macro so the person can just press a button and it deletes these excess columns for them.
Is this possible? Not sure if it makes it easier but could get the header to return a certain result rather than leaving blank, e.g. in columns F-Q, using the above example, the header would be ‘DELETE’. Could I then get a macro to search for any columns that show DELETE in the header and delete the entire column?
Thank you in advance!
-
poorna
May 14, 2016 at 10:29 PM — ReplyHI Sir,
I want to delete non specific columns to be delete
i have 3 sheets , each sheets contain 100+ columns , in that i require 15 columns only
i want 15 columns & remaining to be deleted in every sheets at a time.
Its urgent , taking more than 15 min for 3 sheets -
Ramy
October 11, 2016 at 4:18 PM — ReplyAm trying to delete over 2000 columns (replacing worksheet not an option). The VBA code is this:
wsResults.Range(“G2:BLG502”).EntireColumn.Delete
No matter what I do (using ClearContents, Clear, repair spreadhseet, reboot, clear Windows Temp folder), I always get the message: Run-time error ‘1004’: Delete method of range failed.
When I try to break the delete into smaller segments, such as 100 columns at a time, as in:
wsResults.Range(“G2:DB502”).EntireColumn.Delete
wsResults.Range(“DC10:GX502”).EntireColumn.Delete
…I get the message after roughly 700 columns.
Am I hitting some sort of internal limitation? Is there some buffer that needs to be reset?
Thanks.
-
vishnu
March 31, 2017 at 6:54 PM — ReplyHi,
Actually i have 100 columns in my excel sheet.I have to delete all empty columns in the sheet [Here, i don’t know which column number is empty].
please help me to resolve this issue.
thanks!
-
Abdul
May 29, 2017 at 8:22 PM — ReplyHello,
Can anyone help me,
I want to delete multiple columns and in many sheets (delete same columns; multiple in all sheets)
-
Kapil kaushal
February 27, 2019 at 2:04 PM — ReplyHi , Can someone help me with the below scenario.
I have a column name “COMMENT” in my excel sheet. However the comment column comes 5 times or more than that.
I need to keep the latest comment column and delete the rest other comment columns. I have tried to achieve this by using Range & cell values but the cell values are not static they are getting changed when i export any other document into excel.
Please advise.
Effectively Manage Your
Projects and Resources
ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.
We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.
Project Management
Excel VBA
Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.
Page load link
Go to Top
-
#1
I would like to clear my data in columns A,B,C in 7 of my worksheets in my workbook.
I need help creating a VBA for that?
Any help is appreciated.
Excel Facts
What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Smitty
Legend
- Joined
- May 15, 2003
- Messages
- 29,536
-
#2
You can record a macro grouping the 7 sheets, then select columns A, B & C on the first sheet and delete.
That would be faster than looping through sheets too.
HTH,
pgc01
MrExcel MVP
- Joined
- Apr 25, 2006
- Messages
- 19,893
-
#3
An example. For the worksheets «Sheet3», «Sheet5», «Sheet7», «Sheet8», try:
Code:
Sub DelCols()
Worksheets("Sheet3").Columns("A:C").Clear
Worksheets(Array("Sheet3", "Sheet5", "Sheet7", "Sheet8")).FillAcrossSheets Worksheets("Sheet3").Columns("A:C")
End Sub
Similar threads
Normally in an Excel worksheet, we have two different methods to delete columns: the keyboard shortcut and the right-click and insert method. But, in VBA, we must use the “Delete” command and the entire column statement to delete any column together. If we need to delete a single column, we give a single column reference, but we give multiple column references for multiple columns.
We perform many actions in Excel like cutting, copying, pasting, adding, deleting, and inserting regularly. We can use all of these actions using VBA coding. However, one of the important concepts we need to learn in VBA is the “deleting column.” This article will show you how to use this “Delete Column” option in VBA.
Table of contents
- Excel VBA Delete Column
- What Does Delete Column Do in Excel VBA?
- Examples of Excel VBA Delete Column Method
- Example #1 – Using Delete Method
- Example #2 – Delete Columns with Worksheet Name
- Example #3 – Delete Blank Columns
- Example #4 – Delete Blank Cells Columns
- Recommended Articles
What Does Delete Column Do in Excel VBA?
As the name says, it will delete the specified column. To perform this task, we must first identify which column to delete. The selection of deleted columns differs from one scenario to another, so that we will cover some of the important and often faced scenarios in this article.
Deleting the columns is easy. First, we need to use the COLUMNS property to select the column, so VBA’s syntax of the “Delete Column” method is below.
Columns (Column Reference).Delete
So, we can construct the code like this:
Columns (2).Delete or Columns (“B”).Delete
It will delete column number 2, i.e., column B.
If we want to delete multiple columns, we cannot enter columns. Instead, we need to reference the columns by column headers, i.e., alphabets.
Columns (“A:D”).Delete
It will delete the column from A to D, i.e., the first 4 columns.
Like this, we can use the “Delete Column” method in VBA to delete particular columns. In the below section, we will see more examples to understand it better. Read on.
Examples of Excel VBA Delete Column Method
Below are examples of deleting columns using VBA.
Example #1 – Using Delete Method
Assume you have the datasheet, something like the below.
If we want to delete the month “Mar,” first select the column property.
Code:
Sub Delete_Example1() Columns( End Sub
Mention the column number or alphabet. In this case, it is either 3 or C.
Code:
Sub Delete_Example1() Columns(3). End Sub
Use the Delete method.
Note: You would not get the IntelliSense list to select the Delete method. Just type “Delete.”
Code:
Sub Delete_Example1() Columns(3).Delete End Sub
Or you can enter the column address like this.
Code:
Sub Delete_Example1() Columns("C").Delete End Sub
Run this code using the F5 key, or you can run it manually and see the result.
Both the codes will do the same job of deleting the mentioned column.
If we want to delete multiple columns, we need to mention them in the alphabet. We cannot use column numbers here.
If we want to delete columns 2 to 4, we can pass the code like the below.
Code:
Sub Delete_Example1() Columns("C:D").Delete End Sub
Run this code manually through the run option or press the F5 key. It will delete the columns “Feb,” “Mar,” and “Apr.”
Example #2 – Delete Columns with Worksheet Name
The above is an overview of how to delete columns using VBA code. However, that is not a good practice to delete columns. Deleting the column without referring to the worksheet name is dangerous.
If you have not mentioned the worksheet name, then whichever sheet is active will delete columns of that sheet.
First, we need to select the worksheet by its name.
Code:
Sub Delete_Example2() Worksheets("Sales Sheet").Select End Sub
After selecting the sheet, we need to select the columns. We can also select the columns by using the VBA RANGE objectRange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more.
Code:
Sub Delete_Example2() Worksheets("Sales Sheet").Select Range("B:D").Delete End Sub
It will delete columns B to D of the worksheet “Sales Sheet.” For this code, it does not matter which is active. Still, it will delete the mentioned columns of that sheet only.
We can construct the VBA codeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more in the single line itself.
Code:
Sub Delete_Example2() Worksheets("Sales Sheet").Range("B:D").Delete End Sub
It also deletes the columns “B to D” without selecting the worksheet “Sales Sheet.”
Example #3 – Delete Blank Columns
Assume you have data that has alternative blank columns like the below.
So, delete every alternate column. Then, we can use the below code.
Code:
Sub Delete_Example3() Dim k As Integer For k = 1 To 4 Columns(k + 1).Delete Next k End Sub
Run this code using the F5 key or manually. Then, it will delete all the alternative blank columns, and our data will look like this.
Note: This works only for alternative blank columns.
Example #4 – Delete Blank Cells Columns
Now, look at this example. In certain situations, we need to delete the entire column if we find any blank cells in the data range. Consider the below data for an example.
All the yellow-colored cells are blank. So here, we require to delete all the blank cell columns. The below code will do that.
Code:
Sub Delete_Example4() Range("A1:F9").Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.EntireColumn.Delete End Sub
Let me explain this code line by line for you.
Our data is from A1 to F9, so first, we must select that range. The below code will do that.
Range("A1:F9").Select
We need to select the blank cells in this selected range of cells. So, to select a blank cell, we need a special cell property. In that property, we have used cell type as blank.
Selection.SpecialCells(xlCellTypeBlanks).Select
Next, it will select all the blank cells, and we are deleting the entire selection column in the selection.
Selection.EntireColumn.Delete
So, our result will look like this.
Wherever it has found the blank cell, it has deleted those blank cells entirely.
You can download this Excel VBA Delete Column here – VBA Delete Column Template
Recommended Articles
This article has been a guide to VBA Delete Column. Here, we learn four methods to delete columns using Excel VBA code, practical examples, and downloadable codes. Below are some useful Excel articles related to VBA: –
- How to Delete File in VBA?
- VBA Integer Function
- IsEmpty Function in VBA
- IFERROR in VBA
Метод Range.Clear для полной очистки диапазона ячеек из кода VBA Excel. Методы очистки отдельных свойств и их групп в ячейках. Примеры использования.
Методы очистки ячеек
Метод | Очищаемые свойства | Примечание |
---|---|---|
Range.Clear | Почти все свойства | Ширина и высота ячеек не изменяются |
Range.ClearComments | Комментарии | Для Excel в составе Office 365 |
Range.ClearContents | Формулы и значения | Исходное форматирование сохраняется |
Range.ClearFormats | Свойства, задающие форматы | В том числе отмена объединения ячеек |
Range.ClearHyperlinks | Гиперссылки | Текст и форматирование сохраняются |
Range.ClearNotes | Примечания и заметки | Примечания – для локальных программ Excel, заметки – для Excel в составе Office 365 |
Range.ClearOutline | Структура данных | Смотрите, что такое структурирование данных |
Range – выражение, возвращающее диапазон ячеек.
Примеры использования
1. Удаление гиперссылки из ячейки A1
Cells(1, 1).ClearHyperlinks
2. Очистка диапазона A1:L50 от формул и значений
Range("A1:L50").ClearContents
3. Очистка всех свойств ячеек в столбцах A:K
Columns("A:K").Clear
4. Очистка форматирования ячеек в строках 1:20
Rows("1:20").ClearFormats
Методы очистки диапазонов ячеек в VBA Excel возвращают очищаемые свойства ячеек к значениям по умолчанию. К таким, как на вновь созданном стандартном рабочем листе. При любых методах очистки высота строк и ширина столбцов не изменяются.
Фразы для контекстного поиска: очистка ячеек, очистка ячейки, очистка формул, очистка от формул, удаление формул, очистка значений, удаление значений, очистка форматов, удаление форматирования, удаление форматов.