A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

macro deletes wrong columns



 
 
Thread Tools Display Modes
  #1  
Old October 6th, 2009, 04:08 PM posted to microsoft.public.excel.misc
Wanna Learn
external usenet poster
 
Posts: 340
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  
Old October 6th, 2009, 04:15 PM posted to microsoft.public.excel.misc
eduardo
external usenet poster
 
Posts: 2,131
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  
Old October 6th, 2009, 04:20 PM posted to microsoft.public.excel.misc
Luke M
external usenet poster
 
Posts: 2,672
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  
Old October 6th, 2009, 04:34 PM posted to microsoft.public.excel.misc
Jim Thomlinson
external usenet poster
 
Posts: 2,641
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

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:49 PM.


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