Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 318
Default macro deletes wrong columns

Hello I have excel 2003 . Every day I receive a file and I have to delete
some columns. so I created a simple macro to delete the columns I do not
need.
Sub NewInv()
'
' Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("D:F").Select
Selection.Delete Shift:=xlToLeft
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("H:I").Select
Selection.Delete Shift:=xlToLeft
Columns("J:L").Select
Selection.Delete Shift:=xlToLeft
Columns("M:M").Select
Selection.Delete Shift:=xlToLeft
Columns("N:O").Select
Selection.Delete Shift:=xlToLeft
End Sub
But when I run the macro the next day it deletes the wrong columns
Please help thanks
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,276
Default macro deletes wrong columns

Hi,
I think the problem is when you select the 3rd set of columns you put D:F
and then you select again F:G, the first set should be D:E

"Wanna Learn" wrote:

Hello I have excel 2003 . Every day I receive a file and I have to delete
some columns. so I created a simple macro to delete the columns I do not
need.
Sub NewInv()
'
' Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("D:F").Select
Selection.Delete Shift:=xlToLeft
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("H:I").Select
Selection.Delete Shift:=xlToLeft
Columns("J:L").Select
Selection.Delete Shift:=xlToLeft
Columns("M:M").Select
Selection.Delete Shift:=xlToLeft
Columns("N:O").Select
Selection.Delete Shift:=xlToLeft
End Sub
But when I run the macro the next day it deletes the wrong columns
Please help thanks

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,722
Default macro deletes wrong columns

Without the knowledge of which columns you actually want deleted, we can only
haphazard a guess.

Since you are deleting columns and "shifting" the remaining columns, this
may be causing you to delete the wrong things. For instance, if, before you
started deleting, your intent was to delete column A & C, once you delete
column A, column C became column B.

Instead of doing multiple steps, select all the columns in one shot and then
delete

example:
Range("A:A,B:C,F:F").Select
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Wanna Learn" wrote:

Hello I have excel 2003 . Every day I receive a file and I have to delete
some columns. so I created a simple macro to delete the columns I do not
need.
Sub NewInv()
'
' Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("D:F").Select
Selection.Delete Shift:=xlToLeft
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("H:I").Select
Selection.Delete Shift:=xlToLeft
Columns("J:L").Select
Selection.Delete Shift:=xlToLeft
Columns("M:M").Select
Selection.Delete Shift:=xlToLeft
Columns("N:O").Select
Selection.Delete Shift:=xlToLeft
End Sub
But when I run the macro the next day it deletes the wrong columns
Please help thanks

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default macro deletes wrong columns

Start with the right most columns and move to the left as you go something
like this.

Sub DeleteColumns()
Columns("N").Delete
Columns("L").Delete
End Sub

If you work left to right as you delete all of the columns to the right get
shifted and it is not obvious what is going to get deleted by just looking at
the code.

--
HTH...

Jim Thomlinson


"Wanna Learn" wrote:

Hello I have excel 2003 . Every day I receive a file and I have to delete
some columns. so I created a simple macro to delete the columns I do not
need.
Sub NewInv()
'
' Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("D:F").Select
Selection.Delete Shift:=xlToLeft
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("H:I").Select
Selection.Delete Shift:=xlToLeft
Columns("J:L").Select
Selection.Delete Shift:=xlToLeft
Columns("M:M").Select
Selection.Delete Shift:=xlToLeft
Columns("N:O").Select
Selection.Delete Shift:=xlToLeft
End Sub
But when I run the macro the next day it deletes the wrong columns
Please help thanks

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
Macro deletes row in range, macro then skips the row moved up steven.holloway Excel Discussion (Misc queries) 8 June 11th 08 11:40 AM
MACRO which deletes a row, when it finds a specific number Spiros Excel Discussion (Misc queries) 3 December 4th 07 05:43 AM
Excel Charting Macro That deletes Sheet Dave_Tho1968 Excel Discussion (Misc queries) 3 October 15th 07 09:02 PM
Macro that deletes every third row....+ ajjag Excel Discussion (Misc queries) 4 June 27th 06 06:03 PM
Renaming and saving workbooks but macro still deletes shrek Excel Worksheet Functions 0 November 8th 05 05:43 PM


All times are GMT +1. The time now is 11:08 PM.

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"