ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need help: search and delete columns (https://www.excelbanter.com/excel-programming/361861-need-help-search-delete-columns.html)

[email protected]

Need help: search and delete columns
 
I'm working in a huge excel spreadsheet (AA:EN). I need to delete all
but 15 columns. How can I create a macro which will search for the
column name and then delete that entire column. Also if the column is
not found how can I program the macro to move to the next
search/delete? Anyway help would be greatly appreciated.

So far this is what I have, but I want to delete the columns without
specifying the Column location as these columns could be in different
locations due to formating issues on another spread sheet.

Cells.Find(What:="private label", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
_
MatchCase:=False).Activate
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Cells.Find(What:="alternate", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
_
MatchCase:=False).Activate
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("D12").Select
Cells.Find(What:="purchase", After:=ActiveCell, LookIn:=xlFormulas,
_
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
_
MatchCase:=False).Activate
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
End Sub


Brian Taylor

Need help: search and delete columns
 
Dim rng as range

For each rng in Range("A1:EN1")
If rng.value = "alternate" then
rng.entirecolumn.delete
End if
Next rng

This assumes that your column header labels are in A1:EN1.


Brian Taylor

Need help: search and delete columns
 
Sorry I didn't see you had three different column headers to be
deleted:

Dim rng as range

For each rng in Range("A1:EN1")
Select case rng.value
case "private label","alternate","purchase"
rng.entirecolumn.delete
End Select
Next rng



All times are GMT +1. The time now is 10:23 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com