Rabu, 11 Januari 2012

[ExcelVBA] Annother Puzzle

 

I want to copy several cells from one sheet to a different sheet in the same workbook. I recorded the following macro:
Sheets("Sheet1").Select
Range("A3:C3").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A7").Select
ActiveSheet.Paste

To simplify this code, I collapsed the first 3 lines into a single line
Sheets("Sheet1").Range("A3:C3").Copy

and the next 2 lines into:
Sheets("Sheet2").Range("A7").Select

and didn't change the last line. So the final macro is
Sheets("Sheet1").Range("A3:C3").Copy
Sheets("Sheet2").Range("A7").Select
ActiveSheet.Paste

This new macro works perfectly if when I start it Sheet2 is the active sheet, but it fails if Sheet1 is the active sheet, with the error message:
Application-defined or object-defined error

I should say that I remember the lesson Paul taught me about placing code in the appropriate module. This code is in "This Workbook" module.
Can anyone explain this?
Thanks.


__._,_.___
Recent Activity:
----------------------------------
Be sure to check out TechTrax Ezine for many, free Excel VBA articles! Go here: http://www.mousetrax.com/techtrax to enter the ezine, then search the ARCHIVES for EXCEL VBA.

----------------------------------
Visit our ExcelVBA group home page for more info and support files:
http://groups.yahoo.com/group/ExcelVBA

----------------------------------
More free tutorials and resources available at:
http://www.mousetrax.com

----------------------------------
MARKETPLACE

Stay on top of your group activity without leaving the page you're on - Get the Yahoo! Toolbar now.

.

__,_._,___

Tidak ada komentar:

Posting Komentar