Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default VBA code that only runs when a worksheet is active

I've got a simple copy and paste special procudure that runs fine as long as
the worksheet is active. But it I try to run it from a command button on
another worksheet, it crashes with the following error:

"Run-time error 1004: Application-defined or object-defined error."

Code execution stops on the second line below:

Worksheets("apps").Range("A2:B2").Copy
Worksheets("apps").Range(Cells(3, 1), Cells(numRows, 2)).PasteSpecial
Paste:=xlPasteValues

The thing that puzzles me about this is that I could understand why I would
get an error if I was referring to this worksheet as "ActiveSheet" or if I
was using the Select method to select the ranges, but I'm not. I'm
referring to the worksheet explicitly by name in the Worksheets collection,
and I'm not using the Select method anywhere in this Sub procedure.

Can anyone tell me why I would need to have this worksheet be active in
order for this code to run, and also how I should modify the above code so
it will run when that worksheet is not active?

Thanks in advance.

Paul


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default VBA code that only runs when a worksheet is active

Paul

try:

Sub test()
Dim numrows As Long
numrows = 5
With Worksheets("apps")
.Range("A2:B2").Copy
.Range(.Cells(3, 1), _
.Cells(numrows, 2)).PasteSpecial _
Paste:=xlPasteValues
End With
End Sub

Regards

Trevor


"Paul James" wrote in message
...
I've got a simple copy and paste special procudure that runs fine as long

as
the worksheet is active. But it I try to run it from a command button on
another worksheet, it crashes with the following error:

"Run-time error 1004: Application-defined or object-defined error."

Code execution stops on the second line below:

Worksheets("apps").Range("A2:B2").Copy
Worksheets("apps").Range(Cells(3, 1), Cells(numRows, 2)).PasteSpecial
Paste:=xlPasteValues

The thing that puzzles me about this is that I could understand why I

would
get an error if I was referring to this worksheet as "ActiveSheet" or if I
was using the Select method to select the ranges, but I'm not. I'm
referring to the worksheet explicitly by name in the Worksheets

collection,
and I'm not using the Select method anywhere in this Sub procedure.

Can anyone tell me why I would need to have this worksheet be active in
order for this code to run, and also how I should modify the above code so
it will run when that worksheet is not active?

Thanks in advance.

Paul




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default VBA code that only runs when a worksheet is active

Hi Paul,

I think the problem is your use of the unqualified Cells method in the
following line:

Worksheets("apps").Range(Cells(3, 1), Cells(numRows, 2)).PasteSpecial

If you don't qualify the Cells method with the worksheet it's supposed to
refer to then it refers to the active worksheet by default. Therefore, if
the apps worksheet is not active when this line of code is run, the Range
method and the Cells methods above are referring to two different worksheet.
Try it like this:

With Worksheets("apps")
.Range("A2:B2").Copy
.Range(.Cells(3, 1), .Cells(numRows, 2)).PasteSpecial
Paste:=xlPasteValues
End With

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"Paul James" wrote in message
...
I've got a simple copy and paste special procudure that runs fine as long

as
the worksheet is active. But it I try to run it from a command button on
another worksheet, it crashes with the following error:

"Run-time error 1004: Application-defined or object-defined error."

Code execution stops on the second line below:

Worksheets("apps").Range("A2:B2").Copy
Worksheets("apps").Range(Cells(3, 1), Cells(numRows, 2)).PasteSpecial
Paste:=xlPasteValues

The thing that puzzles me about this is that I could understand why I

would
get an error if I was referring to this worksheet as "ActiveSheet" or if I
was using the Select method to select the ranges, but I'm not. I'm
referring to the worksheet explicitly by name in the Worksheets

collection,
and I'm not using the Select method anywhere in this Sub procedure.

Can anyone tell me why I would need to have this worksheet be active in
order for this code to run, and also how I should modify the above code so
it will run when that worksheet is not active?

Thanks in advance.

Paul




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default VBA code that only runs when a worksheet is active

The Range property usage is fine. It is the Cells property is what is
missing the explicit reference:

Worksheets("apps").Range(Worksheets("apps").Cells( 3, 1),
Worksheets("apps").Cells(numRows, 2)).PasteSpecial Paste:=xlPasteValues

You might find it easier to define an object variable for the worksheet and
use that instead:
********
Dim wks as Worksheet

Set wks = Worksheets("apps")
wks.Range("A2:B2").Copy
wks.Range(wks.Cells(3, 1), wks.Cells(numRows, 2)).PasteSpecial
Paste:=xlPasteValues
*******
Hope this helps,
--
George Nicholson

Remove 'Junk' from return address.


"Paul James" wrote in message
...
I've got a simple copy and paste special procudure that runs fine as long

as
the worksheet is active. But it I try to run it from a command button on
another worksheet, it crashes with the following error:

"Run-time error 1004: Application-defined or object-defined error."

Code execution stops on the second line below:

Worksheets("apps").Range("A2:B2").Copy
Worksheets("apps").Range(Cells(3, 1), Cells(numRows, 2)).PasteSpecial
Paste:=xlPasteValues

The thing that puzzles me about this is that I could understand why I

would
get an error if I was referring to this worksheet as "ActiveSheet" or if I
was using the Select method to select the ranges, but I'm not. I'm
referring to the worksheet explicitly by name in the Worksheets

collection,
and I'm not using the Select method anywhere in this Sub procedure.

Can anyone tell me why I would need to have this worksheet be active in
order for this code to run, and also how I should modify the above code so
it will run when that worksheet is not active?

Thanks in advance.

Paul




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default many thanks to Trevor, Rob and George

Ah, the finer points of VBA. You were right gentlemen, I needed to insert
an explicit reference to the worksheet so that VBA would understand which
worksheet contained the Cells.

Thank you for showing me how to overcome that problem, and for illuminating
this subtle point about object references.

All my best,

Paul


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
vba code runs...need spaces ........ Wanna Learn Excel Discussion (Misc queries) 2 January 20th 09 12:44 PM
What is the code for the active worksheet? Dr Dan[_2_] Excel Discussion (Misc queries) 12 February 23rd 07 03:12 PM
Code to check for active worksheet KimberlyC Excel Programming 2 November 5th 03 02:49 AM
Code runs every other time Mike Waldron Excel Programming 1 September 27th 03 03:54 PM
How to keep from going dizzy when my code runs TBA[_2_] Excel Programming 3 September 23rd 03 11:33 PM


All times are GMT +1. The time now is 11:53 PM.

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

About Us

"It's about Microsoft Excel"