Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Copy Destination

HI,

The fowling code does not work as designed

Sheets("Raw Data").Range(Cells(7, Mycol), Cells(397, Mycol + 4)).Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)

However if I changed it to
Sheets("Raw Data").Range("B7:F397").Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)

It works.


MyCol should = 2 because a Find determined that the item I was looking
for is in column 2.



Any help with this problem would be appreciated.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default Copy Destination

Hi Charles

Here is an example of how it should work using variables which you can
change to suit. Just change the workbook name you are pasting the
data to and the variable which gives you the column you require.

Take care

Marcus

Option Explicit

Sub Copy()
Dim Mycol As Integer
Dim MyWkBook As Workbook
Dim Myrng As Range

Mycol = Cells.Find(What:="Test").Column
Set MyWkBook = Workbooks("Book1") 'Change WBook Name
Set Myrng = MyWkBook.Sheets("Raw Data").Range("A1")

Sheets("Raw Data").Range(Cells(7, Mycol), _
Cells(397, Mycol + 4)).Copy Myrng
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Copy Destination

If you are running the code from a control on a worksheet or a form then it
will be necessary to qualify your Cells references to the specific
worksheet.

With Sheets("Raw Data")
.Range(.Cells(7, Mycol), .Cells(397, Mycol + 4)).Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)
End With

Note the dots in front of the Cells keyword that tie the cells directly to
the sheet named in the With statement. Otherwise the Cells reference
reverts to the object containing the code and will either produce an error
or the wrong results.


"Charles" wrote in message
...
HI,

The fowling code does not work as designed

Sheets("Raw Data").Range(Cells(7, Mycol), Cells(397, Mycol + 4)).Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)

However if I changed it to
Sheets("Raw Data").Range("B7:F397").Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)

It works.


MyCol should = 2 because a Find determined that the item I was looking
for is in column 2.



Any help with this problem would be appreciated.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Copy Destination

The problem likely lies in the way you are using Cells.

Sheets("Raw Data").Range(Cells(7, Mycol), Cells(397, Mycol + 4))


Here, you are trying create a range on worksheet 'Raw Data' with

Sheets("Raw Data").Range


but you have not instructed the Cells method which worksheet they are
to refer.

Cells(7, Mycol)


This Cells call does not necessarily point to sheet 'Raw Data'. It
might, if Raw Data is the active sheet. But if any other sheet is
active, it will point to that sheet, and thus you are attempting to
create a range on Raw Data using ranges on another sheet. This
obviously won't do, so you get an error. If the code is in one of the
Sheet modules, the Cells reference will point to that worksheet,
regardless of what sheet is active.

A much better way is to use a With statement and qualify all the
references to Raw Data with leading periods.

With Sheets("Raw Data")
.Range(.Cells(7, Mycol), .Cells(397, Mycol + 4)).Copy _
Destination:=whatever
End With

Pay close attention to the leading periods. They are very important
because they point whatever follows them to the object in the With
statement.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]





On Sat, 13 Feb 2010 12:08:03 -0800 (PST), Charles
wrote:

HI,

The fowling code does not work as designed

Sheets("Raw Data").Range(Cells(7, Mycol), Cells(397, Mycol + 4)).Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)

However if I changed it to
Sheets("Raw Data").Range("B7:F397").Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)

It works.


MyCol should = 2 because a Find determined that the item I was looking
for is in column 2.



Any help with this problem would be appreciated.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Copy Destination

On Feb 13, 5:39*pm, Chip Pearson wrote:
The problem likely lies in the way you are using Cells.

Sheets("Raw Data").Range(Cells(7, Mycol), Cells(397, Mycol + 4))


Here, you are trying create a range on worksheet 'Raw Data' with

Sheets("Raw Data").Range


but you have not instructed the Cells method which worksheet they are
to refer.

Cells(7, Mycol)


This Cells call does not necessarily point to sheet 'Raw Data'. It
might, if Raw Data is the active sheet. But if any other sheet is
active, it will point to that sheet, and thus you are attempting to
create a range on Raw Data using ranges on another sheet. This
obviously won't do, so you get an error. If the code is in one of the
Sheet modules, the Cells reference will point to that worksheet,
regardless of what sheet is active.

A much better way is to use a With statement and qualify all the
references to Raw Data with leading periods.

With Sheets("Raw Data")
* * * * .Range(.Cells(7, Mycol), .Cells(397, Mycol + 4)).Copy_
* * * * * * * *Destination:=whatever
End With

Pay close attention to the leading periods. They are very important
because they point whatever follows them to the object in the With
statement.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLCwww.cpearson.com
[email on web site]

On Sat, 13 Feb 2010 12:08:03 -0800 (PST), Charles

wrote:
HI,


The fowling code does not work as designed


Sheets("Raw Data").Range(Cells(7, Mycol), Cells(397, Mycol + 4)).Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)


However if I changed it to
Sheets("Raw Data").Range("B7:F397").Copy
Destination:=MyWkBook.Sheets("Raw Data").Range(Myrng)


It works.


MyCol should = 2 because a Find determined that the item I was looking
for is in column 2.


Any help with this problem would be appreciated.


Thanks to all who replayed, sorry for the late feedback. I was taken
down with the "Flu".
Chip's answered my problem, but also learned from the the other
replays.

Thanks again.
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
Copy destination Francis Hookham Excel Programming 4 April 17th 07 02:01 PM
Help with copy destination David Excel Programming 3 July 26th 06 09:51 AM
VBA .copy destination jerredjohnson Excel Discussion (Misc queries) 2 July 18th 06 11:18 PM
Copy Destination fails L. Howard Kittle Excel Programming 2 March 16th 06 12:58 AM
Selection.Copy Destination:= Mike Fogleman Excel Programming 3 January 10th 04 02:18 AM


All times are GMT +1. The time now is 01:42 AM.

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"