Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default Code to delete columns meeting a condition

I need to compare cells I2:BE2 with cell H1 and delete the columns which are
greater than H1.

I'm sure it's simple, but I'm just learning.
--
CG
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 427
Default Code to delete columns meeting a condition

I got results with this:

Sub DeleteColumnsGreaterThanH1()
Range("be2").Select
Do While Selection.Column 8
If ActiveCell.Value Range("h1").Value Then
Selection.EntireColumn.Delete
ActiveCell.Offset(0, -1).Select
Loop
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 427
Default Code to delete columns meeting a condition

FYI
I looked at the posted version of my message, and one of the lines
wrapped and continued on the next line. If you copy and paste it as I
see it into your VBA editor, the code will give compile errors to the
effect of "if with no end if".

This line of code:
If ActiveCell.Value Range("h1").Value Then

should be followed on the same line with
Selection.EntireColumn.Delete

  #4   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default Code to delete columns meeting a condition

I got a "Loop without Do" error.
--
CG


"Dave O" wrote:

I got results with this:

Sub DeleteColumnsGreaterThanH1()
Range("be2").Select
Do While Selection.Column 8
If ActiveCell.Value Range("h1").Value Then
Selection.EntireColumn.Delete
ActiveCell.Offset(0, -1).Select
Loop
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 22,906
Default Code to delete columns meeting a condition

You need an End If

Sub DeleteColumnsGreaterThanH1()
Range("be2").Select
Do While Selection.Column 8
If ActiveCell.Value Range("h1").Value Then
Selection.EntireColumn.Delete
ActiveCell.Offset(0, -1).Select
End If
Loop
End Sub


Gord Dibben MS Excel MVP

On Fri, 2 Feb 2007 14:51:01 -0800, CEG wrote:

I got a "Loop without Do" error.




  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 427
Default Code to delete columns meeting a condition

With all due respect to all contributors: my original post did not
contain an End If because the IF structure had only one instruction.
Unfortunately the newsgroup line-wrapped, as I explained earlier;
adding the End If prevents the Offset from occurring on every loop,
which is required to consider all columns in that range.

CEG, please concatenate the two lines indicated in my responding post.

  #7   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default Code to delete columns meeting a condition

Thanks so much...this works!
--
CG


"Dave O" wrote:

With all due respect to all contributors: my original post did not
contain an End If because the IF structure had only one instruction.
Unfortunately the newsgroup line-wrapped, as I explained earlier;
adding the End If prevents the Offset from occurring on every loop,
which is required to consider all columns in that range.

CEG, please concatenate the two lines indicated in my responding post.


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
copy and use vba code Giz Excel Discussion (Misc queries) 9 November 22nd 06 09:57 PM
Search & delete ANY text in select columns huruta Excel Discussion (Misc queries) 2 June 30th 06 07:31 PM
delete unused columns and grey out the rest area random number generator Excel Worksheet Functions 1 March 31st 06 06:32 PM
merge text from 2 columns into 1 then delete the old 2 columns sleepindogg Excel Worksheet Functions 4 March 30th 06 07:25 PM
Can you delete all hidden columns in an area in one move? Gizmofied Excel Discussion (Misc queries) 2 February 2nd 06 10:41 PM


All times are GMT +1. The time now is 07:33 PM.

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

About Us

"It's about Microsoft Excel"