Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting Column Based On Header
I have a spreadsheet that people keep adding columns to. I import this sheet
and I have a macro that I hide the unwanted columns. The problem is that I have to re-write the macro every time they add another (or change) column. I need to write a macro that will delete the unwanted columns without re-writing the macro. EX: "Column1" "Column2 "Column3" I want to keep Column1 and Column3 and delete Column2 no matter what its name or possition. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting Column Based On Header
Name the keepers and write the code to delete all columns except the keepers.
"David A." wrote: I have a spreadsheet that people keep adding columns to. I import this sheet and I have a macro that I hide the unwanted columns. The problem is that I have to re-write the macro every time they add another (or change) column. I need to write a macro that will delete the unwanted columns without re-writing the macro. EX: "Column1" "Column2 "Column3" I want to keep Column1 and Column3 and delete Column2 no matter what its name or possition. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting Column Based On Header
Cool, thats what I am having problems with. What would that look like. I keep
try IF statements. How do I get it to look for the names without choseing a cell, A1..... A2.... I wont really know what column the names will be in. "JLGWhiz" wrote: Name the keepers and write the code to delete all columns except the keepers. "David A." wrote: I have a spreadsheet that people keep adding columns to. I import this sheet and I have a macro that I hide the unwanted columns. The problem is that I have to re-write the macro every time they add another (or change) column. I need to write a macro that will delete the unwanted columns without re-writing the macro. EX: "Column1" "Column2 "Column3" I want to keep Column1 and Column3 and delete Column2 no matter what its name or possition. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting Column Based On Header
Assume that you name three columns "Ralph", "Irvin" and "Melvin" respectively.
lc = Cells(1,Columns.Count).End(xlToLeft).Column myRng = Range("A1", Cells(1, lc)) For Each C In myRng If C < "Ralph" Or .Name < "Irvin" Or C.Name < "Melvin" Then C.EntireColumn.Delete End If Next I didn't test this so you should before you install it in your regular code. "David A." wrote: I have a spreadsheet that people keep adding columns to. I import this sheet and I have a macro that I hide the unwanted columns. The problem is that I have to re-write the macro every time they add another (or change) column. I need to write a macro that will delete the unwanted columns without re-writing the macro. EX: "Column1" "Column2 "Column3" I want to keep Column1 and Column3 and delete Column2 no matter what its name or possition. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting Column Based On Header
David, you might have to change the Or to And for that to work right. I get
confused on the logic of those sometimes when evaluating negative conditions. "David A." wrote: I have a spreadsheet that people keep adding columns to. I import this sheet and I have a macro that I hide the unwanted columns. The problem is that I have to re-write the macro every time they add another (or change) column. I need to write a macro that will delete the unwanted columns without re-writing the macro. EX: "Column1" "Column2 "Column3" I want to keep Column1 and Column3 and delete Column2 no matter what its name or possition. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Deleting Column Based On Header
Its not liking the If C < "Ralph" Or .Name < "Irvin" Or C.Name < "Melvin"
Then statment. I have change it to and and or but still not working. "JLGWhiz" wrote: Assume that you name three columns "Ralph", "Irvin" and "Melvin" respectively. lc = Cells(1,Columns.Count).End(xlToLeft).Column myRng = Range("A1", Cells(1, lc)) For Each C In myRng If C < "Ralph" Or .Name < "Irvin" Or C.Name < "Melvin" Then C.EntireColumn.Delete End If Next I didn't test this so you should before you install it in your regular code. "David A." wrote: I have a spreadsheet that people keep adding columns to. I import this sheet and I have a macro that I hide the unwanted columns. The problem is that I have to re-write the macro every time they add another (or change) column. I need to write a macro that will delete the unwanted columns without re-writing the macro. EX: "Column1" "Column2 "Column3" I want to keep Column1 and Column3 and delete Column2 no matter what its name or possition. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Return column header based on last value in row | Excel Worksheet Functions | |||
Returning Column Header based on Row and Value | New Users to Excel | |||
Search for a column based on the column header and then past data from it to another column in another workbook | Excel Programming | |||
Find Column to use based on Name of Header | Excel Programming | |||
Deleting a row based on a value in a column | Excel Programming |