Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
copy and use vba code | Excel Discussion (Misc queries) | |||
Search & delete ANY text in select columns | Excel Discussion (Misc queries) | |||
delete unused columns and grey out the rest area | Excel Worksheet Functions | |||
merge text from 2 columns into 1 then delete the old 2 columns | Excel Worksheet Functions | |||
Can you delete all hidden columns in an area in one move? | Excel Discussion (Misc queries) |