0

I am trying to copy a range of cells from one worksheet (Sheet2) to a different range of cells in another worksheet (Sheet1) but when I run the below, I get a run time error (1004) application defined or object defined error. Any help is appreciated!

Option Explicit

Sub deletecolumns()

Workbooks(1).Worksheets(1).Columns(2).Select
Workbooks(1).Worksheets(1).Columns(2).Delete


Sheets("Sheet2").Range("I4:I29").Copy Destination:=Sheets("Sheet1").Range("H4:H29")

End Sub
Community
  • 1
  • 1
xzy
  • 1

1 Answers1

1

delete this line:

Workbooks(1).Worksheets(1).Columns(2).Select

there's no need for it before doing a .delete anyway

FreeMan
  • 5,660
  • 1
  • 27
  • 53
  • 1
    The reason this fails is because you probably do not have the `Worksheets(1)` activated. You are not allowed to `Select` on a `Worksheet` that is not active. – Byron Wall May 29 '15 at 16:17
  • 1
    Thanks for the follow up, @Byron. I avoid `.select` whenever possible, so I don't know all the pitfalls... – FreeMan May 29 '15 at 16:19
  • Unfortunately, we all had to start somewhere. I started with a healthy dose of `Select` `Activate` and `Cells(i,j)` before I learned how to do it proper. – Byron Wall May 29 '15 at 16:22
  • I hear ya. I recommend Macro Recorder to learn how to do things (and figure out esoteric font settings, etc), but also point people to [tips](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) to avoid using select. – FreeMan May 29 '15 at 16:24