Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default Question about how to use range in VBA

Hello,
I am trying to cut and paste one range from one sheet to the same
range in a different sheet. I used the following code:


Range(Cells(startrow, 1), Cells(endrow, 12)).Select
Selection.Copy

Worksheets("SUMM").Activate
Range(Cells(startrow, 1), Cells(endrow, 12)).Select --- ERROR HERE
ActiveSheet.Paste

This code gives me an error where indicated: Application defined or
Object defined error

Notice that I use the exact same line of code three lines earlier and
I get no error. If I change my code to read as

Range(Cells(startrow, 1), Cells(endrow, 12)).Select
Selection.Copy

Worksheets("SUMM").Activate
Range("A12").Select --- NO ERROR
ActiveSheet.Paste

I get no error. What am I doing wrong?

Also, one other question. When I write Range(Cells(startrow, 1), Cells
(endrow, 12)).Select
I get a bunch of highlighted cells. How do I unselect them in VBA?

thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Question about how to use range in VBA

Hi

Not sure why the code gives you an error. Which kind of error do you
get.

Your code can be reduced to this single statement, and then you have
no highlighted cells:

Range(Cells(startrow, 1), Cells(endrow, 12)).Copy Worksheets
("SUMM").Cells(startrow, 1)

Hopes this helps.
....
Per


On 22 Jan., 01:24, Andrew wrote:
Hello,
I am trying to cut and paste one range from one sheet to the same
range in a different sheet. *I used the following code:

Range(Cells(startrow, 1), Cells(endrow, 12)).Select
Selection.Copy

Worksheets("SUMM").Activate
Range(Cells(startrow, 1), Cells(endrow, 12)).Select * --- ERROR HERE
ActiveSheet.Paste

This code gives me an error where indicated: Application defined or
Object defined error

Notice that I use the exact same line of code three lines earlier and
I get no error. *If I change my code to read as

Range(Cells(startrow, 1), Cells(endrow, 12)).Select
Selection.Copy

Worksheets("SUMM").Activate
Range("A12").Select * --- NO ERROR
ActiveSheet.Paste

I get no error. *What am I doing wrong?

Also, one other question. *When I write Range(Cells(startrow, 1), Cells
(endrow, 12)).Select
I get a bunch of highlighted cells. *How do I unselect them in VBA?

thanks


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Question about how to use range in VBA

If your code is in a general module, then the unqualifed ranges refer to the
activesheet.

But I'm guessing that the code is behind a worksheet. That means the
unqualified ranges belong to the sheet that owns the code.

And since you've selected a different sheet, you're trying to select a range on
a non-active sheet.

worksheets("summ").activate
worksheets("Summ").range(worksheets("Summ").cells( startrow, 1), _
worksheets("Summ").cells(endrow,12)).select

will work.

Or

with worksheets("summ")
.activate
.range(.cells(startrow,1), .cells(endrow,12)).select
end with

if you want to save typing.

Remember that you don't need to select an object/range to work with it.


Andrew wrote:

Hello,
I am trying to cut and paste one range from one sheet to the same
range in a different sheet. I used the following code:

Range(Cells(startrow, 1), Cells(endrow, 12)).Select
Selection.Copy

Worksheets("SUMM").Activate
Range(Cells(startrow, 1), Cells(endrow, 12)).Select --- ERROR HERE
ActiveSheet.Paste

This code gives me an error where indicated: Application defined or
Object defined error

Notice that I use the exact same line of code three lines earlier and
I get no error. If I change my code to read as

Range(Cells(startrow, 1), Cells(endrow, 12)).Select
Selection.Copy

Worksheets("SUMM").Activate
Range("A12").Select --- NO ERROR
ActiveSheet.Paste

I get no error. What am I doing wrong?

Also, one other question. When I write Range(Cells(startrow, 1), Cells
(endrow, 12)).Select
I get a bunch of highlighted cells. How do I unselect them in VBA?

thanks


--

Dave Peterson
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
Range question Still Learning Excel Programming 1 June 6th 07 09:22 AM
range name question Gary Keramidas Excel Programming 4 February 21st 06 10:32 PM
Range Question / error 1004: method Range of object Worksheet has failed Paul Excel Programming 3 April 7th 05 02:56 PM
range: question Simon Sunke Excel Programming 2 February 23rd 04 03:25 PM
Range.Formula and Range question using Excel Automation [email protected] Excel Programming 0 September 19th 03 04:53 AM


All times are GMT +1. The time now is 05:20 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"