Perform action in second sheet by clicking cmd button in first she

A

Apurva

I have a command button in sheet 1. I want to delete d first column in sheet
ECO, when the command button is clicked. This is the macro I used:

Sheets("ECO").Select
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft

When I run the macro, I get the error message
"Run-time error 1004-Select method of range class failed"
and the cursor points to the line
Columns("A:A").Select
How can I link the two sheets?
 
J

JE McGimpsey

Apurva said:
I have a command button in sheet 1. I want to delete d first column in sheet
ECO, when the command button is clicked. This is the macro I used:

Sheets("ECO").Select
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft

When I run the macro, I get the error message
"Run-time error 1004-Select method of range class failed"
and the cursor points to the line
Columns("A:A").Select
How can I link the two sheets?

Well, if the code is in a normal code module, it works fine for me.

If it's in a worksheet code module, however, using an unqualified
reference like

Columns("A:A").Select

is equivalent to

Me.Columns("A:A").Select

where Me refers to the worksheet associated with the code module. That
will create the error since you've selected the other sheet.

You can avoid the problem altogether if you deal with the range objects
directly, rather than selections. One way:

Sheets("ECO").Columns("A:A").Delete Shift:=xlToLeft

In addition to working anywhere (in the workbook - you'd need to qualify
that, too, if called from another workbook), it's faster and IMO, easier
to maintain.
 

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

Top