Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return column header based on last value in row C. Excel Worksheet Functions 3 April 12th 10 08:53 AM
Returning Column Header based on Row and Value Chad DiGregorio New Users to Excel 3 July 6th 09 07:09 PM
Search for a column based on the column header and then past data from it to another column in another workbook minkokiss Excel Programming 2 April 5th 07 01:12 AM
Find Column to use based on Name of Header Otto Moehrbach Excel Programming 1 December 7th 06 08:32 PM
Deleting a row based on a value in a column Kai[_2_] Excel Programming 3 November 6th 05 09:19 PM


All times are GMT +1. The time now is 07:14 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"