Macro to delete columns
-
QUESTION: I have several columns of data with column name on row 1. I need to delete some columns with particular name. I don't want to delete by column a, c,f,m.. I want to delete by say "employee_address3", "zipcode" I found this code online. but to delete several columns I have to put several if statements. Is there a simpler way to do this? Case statement or even if statement. Please advise me. Sub DeleteColumnbyName() 'Find last column with data in Row 1 lastCol = Cells(1, Columns.Count).End(xlToLeft).Column 'Loop through columns, starting at the last one For delCol = lastCol To 1 Step -1 'Delete columns with specific Name in Row 1 If Cells(1, delCol) = "employee_address3" Then _ Cells(1, delCol).EntireColumn.Delete Next For delCol = lastCol To 1 Step -1 If Cells(1, delCol) = "zipcode" Then _ Cells(1, delCol).EntireColumn.Delete Next End Sub ANSWER: Peter, Sub DeleteColumnbyName() 'Find last column with data in Row 1 v = Array("employee_address3","zipcode") lastCol = Cells(1, Columns.Count).End(xlToLeft).Column 'Loop through columns, starting at the last one For delCol = lastCol To 1 Step -1 'Delete columns with specific Name in Row 1 bFound = False for i = lbound(v) to Ubound(v) if instr(1,cells(1,delCol),v(i),vbTextcompare) then bFound = True exit for end if Next i If bFound Then _ Cells(1, delCol).EntireColumn.Delete Next delCol End Sub Just put your list of column names in this ar v = Array("employee_address3","zipcode") You can add many v = Array("employee_address3","zipcode","AAA","BBB","FFF","RRR","CCC","DDD") or only use 1 v = Array("employee_address3") they don't have to be in any particular order. ---------- FOLLOW-UP ---------- QUESTION: It works fine except one problem. Say I have to delete column name "people". It deletes that but it also deletes column with name "US_people". How do I avoid that. Basically it should find the exact match of what I want to be deleted? Thank you for your help. I really appreciate it.
-
Answer:
Sub DeleteColumnbyName() 'Find last column with data in Row 1 v = Array("employee_address3","zipcode") lastCol = Cells(1, Columns.Count).End(xlToLeft).Column 'Loop through columns, starting at the last one For delCol = lastCol To 1 Step -1 'Delete columns with specific Name in Row 1 bFound = False for i = lbound(v) to Ubound(v) if instr(1,cells(1,delCol),v(i),vbTextcompare) and _ len(cells(1,delCol)) = len(v(i)) then bFound = True exit for end if Next i If bFound Then _ Cells(1, delCol).EntireColumn.Delete Next delCol End Sub
Miningco.com Visit the source
Related Q & A:
- How to decrypt multiple columns?Best solution by Stack Overflow
- How to skip columns empty in CSV file when importing into MySQL table using LOAD DATA INFILE?Best solution by Stack Overflow
- How do you set just a general macro on your keyboard?Best solution by microsoft.com
- What is micro and macro?Best solution by Yahoo! Answers
- What is the difference between Economics MICRO and MACRO?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.