macro deletes wrong columns

W

Wanna Learn

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
 
E

Eduardo

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
 
L

Luke M

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
 
D

Don Guillett

Work backwards
Without looking too carefully wont this do?

sub delcols()
Columns("C:blush:").delete
Columns("a").delete
end sub
 
J

Jim Thomlinson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro to delete Column 1
vba macro 2
Windows XP apply macros to multiple worksheets 0
Please Help: Macro Nearly Finished 1
code breaks in macro 1
moving all data to cell a1 4
code question 2
Hard reference in a macro 1

Top